1.当只有一个参数时且参数类型是List

List<AnalysisInfo> listInfo(@Param("orderIds") List<Integer> orderIds);
我这里对参数重命名为"orderIds",所以下面foreach中collection="orderIds",如果未重命名则foreach中collection="list"
 1  <select >
 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 >
 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 >
        #{item}
</foreach>
group by materials_id order by totalNum desc limit 5
</select>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
  • 2021-12-27
  • 2021-12-24
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2022-03-03
  • 2021-12-04
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案