在Mybatis中传递集合的方法主要有两种方式:使用List或者使用Map。
public List<User> selectUserByIds(List<Integer> ids); 在XML配置文件中,可以使用foreach标签来遍历List参数,如下所示:
<select id="selectUserByIds" resultType="User"> SELECT * FROM user WHERE id IN <foreach item="item" index="index" collection="ids" open="(" separator="," close=")"> #{item} </foreach> </select> public List<User> selectUserByIds(Map<String, Object> map); 在XML配置文件中,可以通过Map的key来获取集合参数,如下所示:
<select id="selectUserByIds" resultType="User"> SELECT * FROM user WHERE id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </select> 这两种方式都可以有效地传递集合参数给SQL语句,在实际开发中根据需求选择合适的方式。