第一种
在Dao接口中自定义SQL查询,拼接xml字符串
UserDaoMapper.java
@Select("<script>"
+"select * from user where id in"
+ "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'>"
+ "#{id}"
+ "</foreach>"
+ "</script>")
List<User> getUserList(@Param("ids") List<String> ids);
第二种
在Mapper.xml中自定义SQL
UserDaoMapper.xml
<delete id="deleteUserByIds" parameterType="String">
delete from t_user where user_id in
<foreach collection="array" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
</delete>
UserDaoMapper.java
/**
* 批量删除用户
*
* @param ids 需要删除的数据
* @return 结果
*/
public int deleteOperLogByIds(String[] ids);
备注:
标签的collection参数的设置问题,如果参数像上面的例子这样用@Param注解表明了,则用@Param标明的值,否则:
如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以,collection的属性值为Map的key
第三种
List<User> list = this.baseMapper.selectList(new LambdaQueryWrapper<User>().in(User::getUserId,new String[]{"11","22","33","44"}));
List<User> list2 = this.baseMapper.selectList(new LambdaQueryWrapper<User>().in(User::getUserId,new ArrayList<String>(){{this.add("11");this.add("22")}}));
备注:
QueryWrapper + lambda = LambdaQueryWrapper