mybatis动态sql

1. if:    你们能判断,我也能判断!
作为程序猿,谁不懂 if !  在mybatis中也能用 if 啦:

< select id ="findUserById" resultType ="user"> select * from user where < if test ="id != null"> id=#{id} </ if > and deleteFlag=0; </ select >

上面例子: 如果传入的id 不为空, 那么才会SQL才拼接id = #{id}。 这个相信大家看一样就能明白,不多说。
细心的人会发现一个问题:“你这不对啊! 要是你传入的id为null,  那么你这最终的SQL语句不就成了 select * from user where and deleteFlag=0,  这语句有问题!”
是啊,这时候,mybatis的 where 标签就该隆重登场啦:
 
2. where, 有了我,SQL语句拼接条件神马的都是浮云!
咱们通过where改造一下上面的例子:

< select id ="findUserById" resultType ="user"> select * from user
< where > < if test ="id != null"> id=#{id} </ if > and deleteFlag=0; </ where > </ select >

有些人就要问了: “你这都是些什么玩意儿! 跟上面的相比, 不就是多了个where标签嘛! 那这个还会不会出现  select * from user where and deleteFlag=0 ?”
的确,从表面上来看,就是多了个where标签而已, 不过实质上, mybatis是对它做了处理,当它遇到AND或者OR这些,它知道怎么处理。其实我们可以通过 trim 标签去自定义这种处理规则。
 
3. trim :  我的地盘,我做主!
上面的where标签,其实用trim 可以表示如下:
< trim prefix ="WHERE" prefixOverrides ="AND |OR "> ... </ trim >
它的意思就是: 当WHERE后紧随AND或则OR的时候,就去除AND或者OR。 除了WHERE以外, 其实还有一个比较经典的实现,那就是SET。
 
4. set:  信我,不出错!

< update id ="updateUser" parameterType ="com.dy.entity.User"> update user set < if test ="name != null"> name = #{name}, </ if > < if test ="password != null"> password = #{password}, </ if > < if test ="age != null"> age = #{age} </ if > < where > < if test ="id != null"> id = #{id} </ if > and deleteFlag = 0; </ where > </ update >

问题又来了: “如果我只有name不为null,  那么这SQL不就成了 update set name = #{name}, where ........ ?  你那name后面那逗号会导致出错啊!”
是的,这时候,就可以用mybatis为我们提供的set 标签了。下面是通过set标签改造后:

< update id ="updateUser" parameterType ="com.dy.entity.User"> update user
< set > < if test ="name != null"> name = #{name}, </ if > < if test ="password != null"> password = #{password}, </ if > < if test ="age != null"> age = #{age}, </ if > </ set > < where > < if test ="id != null"> id = #{id} </ if > and deleteFlag = 0; </ where > </ update >

这个用trim 可表示为:
< trim prefix ="SET" suffixOverrides =","> ... </ trim >
WHERE是使用的 prefixOverrides(前缀), SET是使用的 suffixOverrides (后缀), 看明白了吧!
 
5. foreach:  你有for, 我有foreach, 不要以为就你才屌!
java中有for, 可通过for循环, 同样, mybatis中有foreach, 可通过它实现循环,循环的对象当然主要是java容器和数组。

< select id ="selectPostIn" resultType ="domain.blog.Post"> SELECT * FROM POST P WHERE ID in < foreach item ="item" index ="index" collection ="list" open ="(" separator ="," close =")"> #{item} </ foreach > </ select >

将一个 List 实例或者数组作为参数对象传给 MyBatis,当这么做的时候,MyBatis 会自动将它包装在一个 Map 中并以名称为键。List 实例将会以“list”作为键,而数组实例的键将是“array”。同样, 当循环的对象为map的时候,index其实就是map的key。
 
6. choose:  我选择了你,你选择了我!
(^(* ̄(oo) ̄)^)你肯定想知道和前面的if有什么区别,好吧,其实我感觉前文if只是判断是否为空,这里呢,不仅判断为空,还有不为空的选择,总之类似于 if...else
Java中有switch,  mybatis有choose。

< 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 >


你能看到我,就说明我们是真爱啊!!!!
有问题的地方请指正,楼主怕水,求轻喷!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值