Mybatis <where>标签
来源:互联网 发布:淘宝开店推广 编辑:程序博客网 时间:2024/05/16 07:55
<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></select>
如果这些条件没有一个能匹配上将会怎样?最终这条 SQL 会变成这样:
SELECT * FROM BLOGWHERE
这会导致查询失败。如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:
SELECT * FROM BLOGWHERE AND title like ‘yiibai.com’
这个查询也会失败。这个问题不能简单的用条件句式来解决,如果你也曾经被迫这样写过,那么你很可能从此以后都不想再这样去写了。
MyBatis 有一个简单的处理,这在90%的情况下都会有用。而在不能使用的地方,你可以自定义处理方式来令其正常工作。一处简单的修改就能得到想要的效果:
<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>
where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。
如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
阅读全文
0 0
- Mybatis where标签
- MyBatis的<where>标签
- Mybatis <where>标签
- Mybatis的<where><if>标签
- MyBatis--动态SQL---where标签
- MyBatis where标签的用法
- mybatis动态SQL的<where>标签
- Mybatis中的三个标签<where> <set> <trim>
- Mybatis的<where><foreach><set>标签详解
- Mybatis动态标签--trim,where,set
- mybatis <where> <set>标签的作用
- Mybatis <where> <if> <set> <trim> <choose>标签
- Mybatis where 1=1 和 <where>标签
- MyBatis中的<where>标签和where子句的区别
- mybatis中sql标签、where标签、foreach标签用法
- Mybatis Where if 标签和Mybatis 模糊查询
- mybatis中<where>标签、<set>标签、<trim>标签、<sql>标签、<foreach>标签的使用
- Mybatis的<where><foreach><set>等标签详解
- ASI(Automatic semicolon insertion-自动分号插入)
- 5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果 A选手说:B第二,我第三; B选手说:我第二,E第四; C选手说:我第一,D第二; D选手说:C最后,我第三; E选手说:我第四,A第一
- 查看Java虚拟机参数值
- XA两阶段提交协议
- 剑指Offer 面试题20:表示数值的字符串 Java代码实现
- Mybatis <where>标签
- VR与AR区别
- qt界面经验记录
- 生物信息-003-安装和使用Entrez Direct
- C语言预习作业
- circuits简单图片分享
- 算法学习推荐
- ajax
- String的'+'的性能及原理