Mybatis使用IN语句查询 关于mybatis in操作使用foreach的问题解决

直接简单粗暴

一 、接口传递对象封装集合属性

下面标记这种形式
collection指的是对象属性myList
例子

User user = new User();
user.setName("必须正解!");
List<String> list = new ArrayList<>();
list.add("1");   
list.add("2");
user.setMyList(list );
接口直接传递User对象进去,属性直接接收就行,就是这个对象你要循环的属性(有其他的where 属性 也可以写进去,不影响)

select * from User where name = #{name} and  id in 

<foreach item="item" index="index" collection="myList" open="("  separator=","  close=")"   >
                #{item}
      </foreach>

二 、接口直接传递集合和数组

不用想了 接口传递的肯定是List集合

<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
	  #{id}
	</foreach>

这肯定就是数组呗

<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
	  #{id}
	</foreach>

问题解决!!!

原文地址:https://www.cnblogs.com/liclBlog/p/15349479.html