mybatis sql语句中 in() 长度为0或null的情况

比如:

select * from A 
where colName IN
<foreach collection="moCodeList" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>

想要查询 colName IN ( 列表) 条件下的数据,如果列表 为null 或者长度为0
语句就变成了 colName IN () 这样的语法是不对的

改进

select * from A 
where colName IN
<if test="moCodeList != null and moCodeList.size>0">
   <foreach collection="moCodeList" item="item" index="index" open="(" close=")" separator=",">
     #{item}
   </foreach>
</if>
<if test="moCodeList==null or moCodeList.size==0">
    and 1=0
</if>

用if 标签 来区分,如果IN 条件里的列表为0 那么,走条件 1=0

相关文章:

  • 2022-12-23
  • 2021-06-10
  • 2022-02-07
  • 2022-02-01
  • 2022-12-23
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
猜你喜欢
  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
相关资源
相似解决方案