mybatis多参数使用方法且其中有的参数是多个值使用in查询
1.当只有一个参数时且参数类型是List
List<AnalysisInfo> listInfo(@Param("orderIds") List<Integer> orderIds);
我这里对参数重命名为"orderIds",所以下面foreach中collection="orderIds",如果未重命名则foreach中collection="list"
1 <select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo"> 2 3 select materials_name as materialsName,sum(num) as totalNum, 4 sum(price) as totalSale 5 from sales_order_detail 6 where shipment_result = 'SUCCESS' and refunds_time is null 7 and sales_order_id in 8 <foreach collection="orderIds" index="index" item="item" open="(" separator="," close=")"> 9 #{item} 10 </foreach> 11 group by materials_id order by totalNum desc limit 5 12 </select>
2. 当只有一个参数时且参数类型是Array
List<AnalysisInfo> listInfo(Long[] orderIds);
如果参数类型是Array则collection属性为array
1 <select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo"> 2 3 select materials_name as materialsName,sum(num) as totalNum, 4 sum(price) as totalSale 5 from sales_order_detail 6 where shipment_result = 'SUCCESS' and refunds_time is null 7 and sales_order_id in 8 <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> 9 #{item} 10 </foreach> 11 group by materials_id order by totalNum desc limit 5 12 </select>
3.注意当查询的参数有多个时,例如
List<AnalysisInfo> listInfo(List<Integer> orderIds, Integer num);
这种情况下传参要使用Map方式,这样在collection属性可以指定名称
Map<String, Object> params = new HashMap<>(); params.put("orderIds",orderIds); params.put("num",num);
List<AnalysisInfo> listInfo(params);
XML如下:
<select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo">
select materials_name as materialsName,sum(num) as totalNum,
sum(price) as totalSale
from sales_order_detail
where shipment_result = 'SUCCESS' and refunds_time is null and num = #{num}
and sales_order_id in
<foreach collection="orderIds" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
group by materials_id order by totalNum desc limit 5
</select>
hello world!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix