<if test="recordTypes!=null">
and d.record_type in
<foreach collection="recordTypes" item="recordType" index="index" open="(" close=")" separator="," >
#{recordType,jdbcType=VARCHAR}
</foreach>
</if>
recordTypes是一个集合,之前因为 collection的值写了"#{recordTypes}",所以就读到了null,上面是正确写法。
mybatis还可以直接遍历类似 “1,21,3,5,99” 这样的字符串,利用split方法将字符串遍历成数组
<if test="entity.id!=null and entity.id!=''">
and t.id in
<foreach collection="entity.id.split(',')" item="id" index="index" open="(" close=")" separator="," >
#{id,jdbcType=NUMERIC}
</foreach>
</if>
本文详细介绍了在MyBatis中如何正确使用foreach标签遍历集合或字符串,并提供了具体的XML配置示例,包括recordTypes集合的正确遍历方式以及如何通过split方法遍历逗号分隔的字符串。
2105

被折叠的 条评论
为什么被折叠?



