MyBatis 多字段IN查询

目标SQL:

select * from demo t where (t.id, t.name) in (('1001', '张三'), ('1002', '李四'));

Mapper接口定义:

//mapper接口定义
List<Map<String, Object>> getResult(@Param("list") List<Map<String, String>> list);

调用持久化层:

//组装数据
List<Map<String, String>> list = Lists.newArrayList();
Map<String, String> map1 = Maps.newHashMap();
map1.put("id", "1001");
map1.put("name", "张三");
list.add(map1);
Map<String, String> map2 = Maps.newHashMap();
map2.put("id", "1002");
map2.put("name", "李四");
list.add(map2);

//调用dao层
List<Map<String, Object>> result = mapper.getResult(list);

MyBatis XML:

<select id="getResult" resultType="java.util.HashMap">
	select * from demo t where (t.id, t.name) in
	<foreach collection="list" item="item" open="(" close=")" separator=",">
		(#{item.id},#{item.name})
	foreach>
select>

你可能感兴趣的:(mybatis)