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