mybatis分割字符串并循环,实现in多个参数的操作

网友投稿 284 2023-01-08

mybatis分割字符串并循环,实现in多个参数的操作

mybatis分割字符串并循环,实现in多个参数

mybatis xml代码:

select *

from carinfo

where

xh in

#{item}

mybatis sql打印:

==> Preparing: select * from carinfo where xh in ( ? , ? )

==> Parameters: 1(String), 2(String)

mybatis多参数使用方法且其中有的参数是多个值使用in查询

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

List listInfo(@Param("orderIds") List orderIds);

我这里对参数重命名为"orderIds",所以下面foreach中collection="orderIds",如果未重命名则foreach中collection="list"

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 sales_order_id in

#{item}

group by materials_id order by totalNum desc limit 5

2. 当只有一个参数时且参数类型是Array

List listInfo(Long[] orderIds);

如果参数类型是Array则collection属性为array

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 sales_order_id in

#{item}

group by materials_id order by totalBXCHQBvUzNum desc limit 5

3.注意当查询的参数有多个时,例如

List listInfo(List orderIds, Integer num);

这种情况下传参要使用Map方式,这样在collection属性可以指定名称

Map params = new HashMap<>();

params.put("orderIds",orderIds);

params.put("num",num);

List listInfo(params);

XML如下:

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

#{item}

group by materials_id order by totalNum desc limit 5

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何让java只根据数据库表名自动生成实体类
下一篇:api接口网站 知乎(网站的api接口)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~