java MySQL的in查询两个字段同时相等的多个数据查询
在Java中想进行下面的查询,不想写循环一条条查
select id,name,address,age from users where name='A' and address='addr1'
select id,name,address,age from users where name='B' and address='addr2'
select id,name,address,age from users where name='C' and address='addr3'
查了下,in可以使用在多个字段上
select id,name,address,age from users where (name,address) in(('A','addr1'),('B','addr2'),('C','addr3'))
在Java中
1.构建条件
List<Map<String, String>> list = new ArrayList<>(); // 创建一个新的Map对象 Map<String, String> map1 = new HashMap<>(); map1.put("name", "A"); map1.put("address", "addr1"); list.add(map1); Map<String, String> map2= new HashMap<>(); map2.put("name", "B"); map2.put("address", "addr2"); list.add(map2); Map<String, String> map3= new HashMap<>(); map3.put("name", "C"); map3.put("address", "addr3"); list.add(map3);
2.mapper接口定义
List<Map<String,Object>> getInfos(@Param("list")List<String> list);
3.mapper XML
<select id="getInfos" resultType="java.util.HashMap"> select id,name,address,age from `users` where (name,address) in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> (#{item.name},#{item.address}) </foreach> </select>
这样就可以一次查询出来了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-05-11 wmic命令行工具使用
2017-05-11 Linux tree命令