foreach
使用List:
<!-- ids为List<Integer> -->
<if test="ids!=null and ids.size()>0">
<!-- 方法1:foreach达到的效果: (t.id=1 or t.id=2 or t.id=3)
and <foreach collection="ids" index="index" item="item" open="(" separator="or" close=")">
t.id=#{item}
</foreach> -->
<!-- 方法2:foreach达到的效果:(1,2,3) -->
and t.id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
使用int[]:
<!-- ids为int[] -->
<if test="ids!=null and ids.length>0">
<!-- 方法1:foreach达到的效果:(t.id=1 or t.id=2 or t.id=3)
and <foreach collection="ids" index="index" item="item" open="(" separator="or" close=")">
t.id=#{item}
</foreach> -->
<!-- 方法2:foreach达到的效果:(1,2,3) -->
and t.id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>