mybatis中in的坑

在mybatis的in查询分为三种传入参数

1、string 字符串传入,以逗号分割:优点:简单方便,高效,缺点:不能防止sql注入,

注意,传入的参数为字符串,如('A123','B123','C123')需要提前把数据封装好

<foreach item="item" index="index" collection="codes.split(',')" open="(" separator="," close=")">
#{item}
</foreach>

in (${item})

 

2、list 集合传入,在in条件中使用,遍历集合

<foreach item="item" index="index" collection="codeList" open="(" close=")" separator=","> 

    #{item}

</foreach>

3、array数组传入,在in条件中使用,遍历数组

<foreach collection="codeArray" item="item" separator="," open="(" close=")">

#{item}

</foreach>

 

posted @ 2023-01-06 18:00  清华大咖  阅读(238)  评论(0)    收藏  举报