mybatis sql語句中 in() 長度為0或null的情況


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM