MyBatis中foreach传入参数为数组
一、当只有一个参数,并且这个参数是数组时
接口方法的参数不需要添加@Param注释,collection="array"
示例
接口的方法
void deleteMulti(String[] flowerids);
xml文件
<delete id="deleteMulti" > delete from cart where flower_id in <foreach collection="array" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>
二、当有多个参数,其中包含数组时
参数需要添加@Param注释
遍历数组参数时,collection="数组参数的Param注释的value值"
示例
接口的方法
void deleteMulti(@Param(value = "names") String[] flowerids,@Param(value = "userid") int userid);
xml文件
<delete id="deleteMulti" > delete from cart where userid = #{userid} and flower_id in <foreach collection="names" item="item" open="(" separator="," close=")"> #{item} </foreach> </delete>