1、<if> 标签:if就是用来对输入映射的字段进行判断,一般是非null判断;
<delete id="deleteSpecResultByTaskIdAndSiteId">
DELETE FROM MIRROR_SPEC_RESULT WHERE TASK_ID =#{taskId,jdbcType=VARCHAR} AND aegis_site_id = #{aegisSiteID,jdbcType=VARCHAR}
<if test="roleName != null and roleName != ''"> and role_name = #{roleName} </if>
(或者模糊查询<if test="roleName != null and roleName != ''"> and role_name like "%"#{roleName}"%" </if>)
</delete>
2、<where> 标签:当所有if条件均不成立的时候,<where>标签不会加上,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。(也就是如果只有第二个条件成立,AND会自动被去掉)
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null"> state = #{state} </if>
<if test="title != null"> AND title like #{title} </if>
<if test="author != null and author.name != null"> AND author_name like #{author.name} </if>
</where>
</select>
3、<set> 标签:set 元素可以用于动态包含需要更新的列
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
用<trim>实现的等效sql
<update id="updateAuthorIfNecessary">
update Author
<trim prefix="SET" suffixOverrides=",">
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</trim>
where id=#{id}
</update>
这里,set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号,因为用了条件语句之后很可能就会在生成的 SQL 语句的后面留下这些逗号。(译者注:因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留)
4、<trim> 标签:2的等效sql
prefix:指添加前缀修饰符;
suffix:添加后缀修饰符;
prefixOverrides : 去掉前缀修饰符;suffixOverrides : 去掉后缀修饰符;
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="state != null"> state = #{state} </if>
<if test="title != null"> AND title like #{title} </if>
<if test="author != null and author.name != null"> AND author_name like #{author.name} </if>
</trim>
</select>
prefixOverrides 属性会忽略通过管道分隔的文本序列(注意此例中的空格也是必要的)。它的作用是移除所有指定在 prefixOverrides 属性中的内容,并且插入 prefix 属性中指定的内容。
5、choose, when, otherwise:
有时我们不想应用到所有的条件语句,而只想从中择其一项。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句,如果 “title” 不为空,就按 ”title“ 查找,如果 “author” 不为空 就按 “author” 查找的情形
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
6、<foreach>:
collection:要遍历的集合;
item:当前正在遍历的对象的变量名;
open:开始遍历;
close:结束遍历;
index:下标;separator:分割;
deleteResultMapper.deleteOldRecordForCmd(lists);
void deleteOldRecordForCmd(@Param("list") List<ResultPojo> list);
<delete id="deleteOldRecordForCmd" parameterType="java.util.List">
DELETE FROM
MIRROR_CMD_RESULT
WHERE
SITE_ID IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item.siteId,jdbcType=INTEGER}
</foreach>
AND
TASK_ID IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item.taskId,jdbcType=VARCHAR}
</foreach>
</delete>
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
注意 你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
7、bind
bind 元素可以从 OGNL 表达式中创建一个变量并将其绑定到上下文。比如:
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>