判断List是否为空
1 2 3 4 5 6 | < if test="list!= null and list.size()>0" > and s.orderstatus in < foreach collection = "list" item = "item" open = "(" close = ")" separator = "," > #{item} </ foreach > </ if > |
一、前言
前一篇博客中介绍了Mybatis的别名的使用,这个方法也是非常方便的。下面小编向大家介绍一下Mybatis的最精彩的亮点——动态SQL。通过mybatis提供的各种标签方法实现动态拼接sql。
二、if
if标签可以起到判断的作用,用来判断我们所要查询的字段是否为空或者是‘’,可以让sql语句更加的灵活。提高了复用性。
PS:注意要做不等于空字符串校验。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- 传递pojo综合查询用户信息 --> < select id = "findUserList" parameterType = "user" resultType = "user" > select * from user where 1=1 < if test = "id!=null and id!=''" > and id=#{id} </ if > < if test = "username!=null and username!=''" > and username like '%${username}%' </ if > </ select > |
如果条件都成立的话,最终的sql语句就是:
1 | select * from user where id=#{id} and username like '%${username}%' |
三、where
Mybatis提供了where,可以自动处理第一个and。
比如在下面的例子中,如果id为不为空或者不为‘’,会自动的判断是否需要and,如果需要的话就补上,如果不需要,Mybatis就自动去掉and。
1 2 3 4 5 6 7 8 9 10 11 | < select id = "findUserList" parameterType = "user" resultType = "user" > select * from user < where > < if test = "id!=null and id!=''" > and id=#{id} </ if > < if test = "username!=null and username!=''" > and username like '%${username}%' </ if > </ where > </ select > |
如果条件都成立的话,最终的sql语句就是:
1 | select * from user where id=#{id} and username like '%${username}%' |
四、foreach
向sql传递数组或List,mybatis使用foreach解析
在用户查询列表和查询总数的statement中增加多个id输入查询。sql语句如下:
两种方法:
1 2 | SELECT * FROM USER WHERE id=1 OR id=10 OR id=16 SELECT * FROM USER WHERE id IN (1,10,16) |
使用 foreach遍历传入ids,他有下面的标签:
- collection:指定输入 对象中集合属性
- item:每个遍历生成对象中
- open:开始遍历时拼接的串
- close:结束遍历时拼接的串
- separator:遍历的两个对象中需要拼接的串
实践代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | < if test = "ids!=null" > <!-- 使用 foreach遍历传入ids collection:指定输入 对象中集合属性 item:每个遍历生成对象中 open:开始遍历时拼接的串 close:结束遍历时拼接的串 separator:遍历的两个对象中需要拼接的串 --> <!-- 使用实现下边的sql拼接: AND (id=1 OR id=10 OR id=16) --> < foreach collection = "ids" item = "user_id" open = "AND (" close = ")" separator = "or" > <!-- 每个遍历需要拼接的串 --> id=#{user_id} </ foreach > <!-- 实现“ AND id IN(1,10,16)”拼接 --> <!-- <foreach collection="ids" item="user_id" open="AND id IN(" close=")" separator=","> 每个遍历需要拼接的串 #{user_id} </foreach> --> </ if > |
五、sql片段
我们可以把上面实现的动态sql判断代码块抽取出来,组成一个sql片段。当其他的statement中需要,就可以引用sql片段了。这样sql代码的复用率就提高了,程序员开发就更加的方便了。
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:
1 2 3 4 5 6 7 8 9 10 11 12 | <!-- 传递pojo综合查询用户信息 --> < select id = "findUserList" parameterType = "user" resultType = "user" > select * from user < where > < if test = "id!=null and id!=''" > and id=#{id} </ if > < if test = "username!=null and username!=''" > and username like '%${username}%' </ if > </ where > </ select > |
将where条件抽取出来:
1 2 3 4 5 6 7 8 | < sql id = "query_user_where" > < if test = "id!=null and id!=''" > and id=#{id} </ if > < if test = "username!=null and username!=''" > and username like '%${username}%' </ if > </ sql > |
Sql片段的引用,使用include引用:
1 2 3 4 5 6 | < select id = "findUserList" parameterType = "user" resultType = "user" > select * from user < where > < include refid = "query_user_where" /> </ where > </ select > |
注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
六、小结
动态SQL就是复用sql语句的过程。
要想体验到这个过程,就必须要多多的尝试一下,必须多多的锻炼,多用,才能把Mybatis的好处发挥到极致。
摘自:https://blog.csdn.net/kisscatforever/article/details/60883304