<if>标签里的and问题
以前经常用 1=1 来解决拼接and问题,
where
1 = 1
<if>and xxx</if>
<if>and xxx</if>
...
现在用<where>标签即可,如下
select
POST_CODE,POST_NAME,REMARK
from POST
<where>
AND POST_CODE = #{postCode}
AND POST_NAME = #{postName}
AND REMARK = #{remark}
</where>
<if>标签直接套在条件外面即可
SELECT
POST_CODE,POST_NAME,REMARK
FROM POST
<where>
<if test="postCode != null and postCode !=''">
AND POST_CODE = #{postCode}
</if>
<if test="postName != null and postName !=''">
AND POST_NAME= #{postName}
</if>
<if test="remark != null and remark!=''">
AND REMARK = #{remark}
</if>
</where>
官方文档
mybatis – MyBatis 3 | 动态 SQL
mybatis中避免where空条件后面添加1=1垃圾条件的 优化方法 <where>标签
mybatis where 1=1导致索引失效问题探究_xinpz的博客-CSDN博客_mybatis 索引失效
更多推荐
mybatis <if>标签 and问题
发布评论