所谓的动态SQL,本职还是SQL语句,只是可以在SQL层面,去执行一个逻辑代码
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性,按照SQL的格式,去排列组合。
建议:
- 先在MySQL中写出完整的SQL,再对应的去修改成为我们的动态SQL实现通用即可。
if标签
根据标签test属性所对应的表达式判断标签中的内容是否拼接到sql中
当第一句sql出错时,会出现异常可以通过添加一个恒成立条件解决
<select id="selectIf" parameterType="po.User" resultType="po.User">
select *from zy where 1=1
<if test="name!=null and job!=null">
and`name`=#{name} and job=#{job}
</if>
<if test="name!=null and job==null">
and `name`=#{name}
</if>
<if test="job!=null and name==null">
and job=#{job}
</if>
</select>
where标签
当where标签中if判断成立,会自动生成where关键字,并且将内容前多余的and或or去掉
当where标签中if判断都不存在,where标签没有任何效果,where元素也会删除
注:where标签不能将and或or放在内容之后。
<select id="UserIf" parameterType="po.User" resultType="po.User">
select *from zy
<where>
<if test="name!=null">
and `name` LIKE "%"#{name}"%" <!--判断成立时会自动消除and -->
</if>
<if test="job!=null">
job LIKE "%"#{job}"%"
</if>
<if test="name==null and job==null">
tel is not NULL
</if>
</where>
</select>
choos(where)标签
类似与switch...case
choose条件中只会满足所有分支的一个
都不满足则执行otherwise中的语句
<select id=" " parameterType=" " resultType=" ">
select *from zy
<where>
<choose>
<when test="name!=null">
and `name` LIKE "%"#{name}"%" <!--判断成立时会自动消除and -->
</when>
<when test="job!=null">
job LIKE "%"#{job}"%"
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
set标签
set元素会动态前置set关键字,同时也会删除无关的逗号
一般用于更新语句
<update id=" ">
update ???
<set>
<if text=" ">username=#{username},</if>
<if text=" ">password=#{password},</if>
</set>
where id=#{id}
</update>
trim标签
trim包括where set标签
<trim prefix="" prefixOverrides="" suffix="" suffixOverrides=""></trim>
prefix:前缀 prefixOverrides:前缀包括什么
suffix:后缀 suffixOverrides:后缀包括什么
<trim prefix="WHERE" prefixOverrides="AND |OR">
</trim>
可以通过自定义trim元素来定制where元素的功能
prefixOverrides属性会通过管道分隔的文本序列(此例中的空格也是必要的)。
它的作用是移除所有在指定prefixOverrides属性中的内容,并且插入prefix属性中的指定的内容。
<trim suffix="SET" suffixOverrides=","></trim>
与前缀类似
<!-- trim重改where-->
<select id="trimWhere" resultType="po.User" parameterType="po.User">
select *from zy
<trim prefix="Where" prefixOverrides="and">
<if test="name!=null">
and `name`=#{name }
</if>
<if test="job!=null">
and job=#{job}
</if>
</trim>
</select>
<--trim重改set-->
<update id="trimSet" parameterType="po.User">
update zy
<trim prefix="set" suffixOverrides=",">
<if test="name!=null"> `name`=#{name},</if>
<if test="job!=null"> job=#{job},</if>
</trim>
where id=#{id}
</update>
动态SQL
有的时候,我们可能会将一些功能的部分抽取出来,方便复用!
1、使用SQL标签抽取公共部分
<sql id="user_id">
<if test="name!=null">
and `name` LIKE "%"#{name}"%"
</if>
<if test="job!=null">
job LIKE "%"#{job}"%"
</if>
<if test="name==null and job==null">
tel is not NULL
</if>
</sql>
2、在需要使用的地方使用include标签引用即可
<select id="UserIf" parameterType="po.User" resultType="po.User">
select *from zy
<where>
<include refid="user_id"></include>
</where>
</select>
注意事项:
- 最好基于单表来定义SQL片段!
- 不要存在where标签
Foreach
select * from user where 1=1 and (id=1 or id=2 or id=3)
对一个集合进行遍历,通常是在构建IN条件语句的时候
<select id="selectForEach" parameterType="list" resultType="po.User">
select * from zy
<where>
<foreach collection="list" item="id" open="(" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
collection是数组名 用于获取数组
item是值 用于引用
open 前缀 close 后缀 or 连接符
id=#{id} 是遍历后输入到sql语句中的句子
简易来说foreach就是一个拼接SQL语句的方法
利用foreach进行多个数据插入
insert into tableName (num1,num2,num3)
values
(1,2,3),
(4,3,4),
(1.23,9); SQL语句
<insert id="insertForEach" parameterType="list">
insert into zy(id,name)values
<foreach collection="list" item="user" separator=",">
(#{user.id},#{user.name})
</foreach>
</insert>
进行一个拼接即可