mybatis foreach 遍历list中的坑
将jdbc改写为mybatis时,传入的条件为list使用到的标签是<where> 、<choose>、<when>、<if>、<foreach>因为判断list集合时判断条件不全,导致sql执行错误,下面是正确的判断条件
<where>
<choose>
<when test="unitList != null and ! unitList.isEmpty() and unitList.size() > 0">
(tab2.id IN
<foreach collection="unitList" item="item" index="index"
open="(" separator="," close=")">
#{item}
</foreach>
AND tab1.`status` = #{deviceStatus})
<if test="zoonList != null and ! zoonList.isEmpty() and zoonList.size() > 0">
OR (tab2.leaderId IN
<foreach collection="zoonList" item="item" index="index"
open="(" separator="," close=")">
#{item}
</foreach>
AND tab1.`status` = #{deviceStatus})
</if>
</when>
<when test="zoonList != null and ! zoonList.isEmpty() and zoonList.size() > 0">
tab2.leaderId IN
<foreach collection="zoonList" item="item" index="index"
open="(" separator="," close=")">
#{item}
</foreach>
AND tab1.`status` = #{deviceStatus}
</when>
</choose>
</where>