MyBatis常用的动态sql标签

date: 2019/8/15 0:29:17


MyBatis的强大特性之一便是它的动态 SQL,提高开发人员的开发效率。



1.if 标签

类似于我们Java中的if  test  里面写条件
1
2
3
4
5
6
7
8
9
<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>

2.choose, when, otherwise 标签

类似我们的 switch   例如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null"> <!--选择上 就退出了 后面就不看了 类似 if else if -->
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise> <!--如果都没有选择上 就执行这个 类似 java switch 中的 default-->
AND featured = 1
</otherwise>
</choose>
</select>

3.trim 标签

trim 格式化标记 例如:

prefix 前缀
suffix 后缀
suffixOverrides 格式化方式 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<trim prefix="(" suffix=")" suffixOverrides=","> <!-- prefix 前缀 , suffix 后缀 , suffixOverrides 格式化方式 -->
<if test="empid != null">
empid,
</if>
<if test="ename != null">
empid,
</if>
<if test="egendar != null"> <!-- 如果条件满足 最后一个 , 就可以帮我们格式化掉 -->
egendar,
</if>
</trim>
<!-- 结果就是 (empid,empid,egendar) 本来拼接的是 (empid,empid,egendar,) egendar, 我的 trim标签帮我们吧 ,去掉了 -->

4.where 标签


where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。
而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。

如果 where 元素没有按正常套路出牌,
我们可以通过自定义 trim 元素来定制 where 元素的功能。
比如,和 where 元素等价的自定义 trim 元素为:
等于说  配合 trim有是一篇天地  两个事例

1
2
3
4
5
<!-- 配合 帮我们 格式 and 或者 or -->
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<where>
<if test="empid != null">
and e.empid = #{empid}
</if>
<if test="ename != null">
and e.ename = #{ename}
</if>
<if test="egendar != null">
and e.egendar = #{egendar}
</if>
</where>
<!-- 模拟输出: where e.empid = #{empid} and e.ename = #{ename} and e.egendar = #{egendar} -->
<!-- 按照if的拼接应该是 : where and e.empid = #{empid} and e.ename = #{ename} and e.egendar = #{egendar} -->
<!-- 当然这是一个错误的 sql语句 where 后 and ? 这。。。。-->
<!-- 我们 where 标签就是 帮我们 格式化 规则 去掉 多余的 and 使他成为 正确的 sql -->

5.set 标签

这个可能用的最多的就是  update

这里,set 元素会动态前置 SET 关键字,
同时也会删掉无关的逗号,
因为用了条件语句之后很可能就会在生成的 SQL 语句的后面留下这些逗号。
(译者注:因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留)


我们来看看  如何使用 强大的 trim 标签实现这个 set (set 就是 去掉多余的  逗号    并且前面帮我们拼接一个 set )
1
2
3
4
<trim prefix="SET" suffixOverrides=",">
...
</trim>

事例 update 配合 set 来试试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
update t_emp
<set>
<if test="ename != null">
ename = #{ename},
</if>
<if test="egendar != null">
egendar = #{egendar},
</if>
</set>
where empid = #{empid}
<!-- 模拟输出 update t_emp set ename = #{ename},egendar = #{egendar} where empid = #{empid} -->
<!-- 不考虑set 输出 update t_emp ename = #{ename},egendar = #{egendar}, where empid = #{empid} -->
<!-- 这是 sql吗? 显然不是 update t_emp 后面的set? 没有 而且 where 前面还有 逗号 #{egendar}, where 这 显然不对-->
<!-- set作用就是 去掉多余的 逗号 对吧 并且前面帮我们拼接一个 set -->

6.foreach 标签

struts2 里面的 s标签 c:foreach
和这个很相似 用法 

collection 循环的集合 
item  (对象别名) (数字 字符 相当于 值)
index 索引 类似 for (int i =0; i<10; i++) 这里的 i

open 前缀
close 后缀
separator 中间用什么隔开

事例
1
2
3
4
5
6
7
8
9
10
11
12
13
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
<!-- ( 1,2,3 ) -->
<!-- ( ) 前后缀 -->
<!-- 123 item 数值 -->
<!-- ,分割的方式 -->

文章目录
  1. 1. date: 2019/8/15 0:29:17
  • 1.if 标签
  • 2.choose, when, otherwise 标签
  • 3.trim 标签
  • 4.where 标签
  • 5.set 标签
  • 6.foreach 标签