Mybatis xml条件查询和第一个条件为空的解决方法

在使用mybatis写SQL语句时,经常遇到要以条件参数的值来查询不同的字段,这里做一个小结:

  <where>
    <!-- 判断条件参数是否为空 -->
    <if test="orgId != 'null' and orgId != ''">
        and user.org_id = #{orgId}
    </if>
     <!-- 判断条件参数是否为某值 -->
    <if test="searchCon == 'cn'">
        and opxuser.name like CONCAT(#{searchCon},'%') 
    </if>
  </where>

这里使用 标签可以避免第一个查询条件为空SQL出错的情况,
比在where后面添加1=1的写法更加优雅。

猜你喜欢

转载自www.cnblogs.com/yzhu/p/10981011.html
今日推荐