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>