Myabtis动态SQL拼接
一、if 标签
if 标签可通过 test 属性的表达式进行判断,若表达式的结果为 true,则执行标签体中的sql语句;反之标签中的内容不会执行。
示例:
<select id="selectUser" resultType="User">
select * from user
<if test="username != null and username != ''">
where username = #{username}
</if>
</select>
二、where 标签
若 where 标签中的 if 条件都不满足,则 where 标签没有任何功能,即不会添加 where 关键字。
若 where 标签中的 if 条件满足,则 where 标签会自动添加 where 关键字,并将条件最前方多余的 and 去掉。
注:where标签会自动去除sql语句中的多余的and或者or,但是不能去掉条件最后多余的 and 示例:
<select id="selectUser" resultType="User">
select * from user
<where>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
</where>
</select>
三、trim 标签
trim 标签用于去掉或添加标签中的内容,它有四个常用的属性,如下:
1、prefix:在 trim 标签中的内容的前面添加某些内容。
2、prefixOverrides:在 trim 标签中的内容的前面去掉某些内容。
3、suffix:在 trim 标签中的内容的后面添加某些内容。
4、suffixOverrides:在 trim 标签中的内容的后面去掉某些内容。 示例:
<select id="selectUser" resultType="User">
select * from user
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
</trim>
</select>
这里可以自行去以下网站查询具体的含义以及效果:https://mybatis.p2hp.com/dynamic-sql.html#trim%E3%80%81where%E3%80%81set
四、set 标签
set 标签用于拼接 update 语句的 set 子句,set 元素可以用于动态包含需要更新的列,忽略其它不更新的列。
注:set标签会自动去除sql语句中的多余的逗号,但是不能去掉条件最后多余的 and
示例:
<update id="updateUser" parameterType="User">
update user
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="age != null and age != ''">
age = #{age}
</if>
</set>
</update>
这个例子中,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)。
五、choose、when、otherwise
choose标签:用于动态选择元素,它和 if...else if...else 的功能类似,但是 choose 本身没有判断条件,它用于将多个 if 语句组合到一起。
when标签:用于指定条件,when标签的test属性为true时,会执行when标签中的内容,否则执行otherwise标签中的内容。
otherwise标签:用于指定默认内容,当when标签中的条件都不满足时,会执行otherwise标签中的内容。
示例:
<select id="selectUser" resultType="User">
select * from user
<choose>
<when test="username != null and username != ''">
where username CONCAT(CONCAT('%',#{username}),'%')
</when>
<when test="sex != null and sex != ''">
where sex = #{sex}
</when>
<otherwise>
where age > 18
</otherwise>
</choose>
</select>
六、foreach 标签
foreach 标签用于遍历集合比如说批量删除或者批量添加,如果前面传过来一个数组或者集合,那么我们就需要使用 foreach 标签进行循环操作,它有以下常用的属性,如下:
- 1、collection:设置需要遍历的数组或集合。
- 2、item:设置遍历过程中集合或数组的每一个元素。
- 3、index:设置遍历过程中集合的索引。
- 4、separator:设置遍历过程中集合的分隔符。
- 5、open:设置遍历过程中集合的开始符号。
- 6、close:设置遍历过程中集合的结束符号。
示例:
public interface UserMapper {
/**
* 根据用户id批量删除用户信息
* @param ids
* @return
*/
int deleteMoreByArray(@Param("ids") int [] ids);
int deleteMoreByList(@Param("list") List<Integer> list);
}
<!-- 批量删除用户 -->
<delete id="deleteMoreByArray" parameterType="array">
delete from user where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<!-- 批量删除用户 -->
<delete id="deleteMoreByList" parameterType="list">
delete from user where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>