这篇文章主要为大家展示了“mybatis分割字符串并循环实现in多个参数的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“mybatis分割字符串并循环实现in多个参数的示例分析”这篇文章吧。
mybatis xml代码:
<select id="selectInXh" resultMap="BaseResultMap" parameterType="java.lang.String"> select * from carinfo where xh in <if test="param1 != null and param1 != ''"> <foreach item="item" index="index" collection="param1.split(',')" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
mybatis sql打印:
==> Preparing: select * from carinfo where xh in ( ? , ? ) ==> Parameters: 1(String), 2(String)
List<AnalysisInfo> listInfo(@Param("orderIds") List<Integer> orderIds);
我这里对参数重命名为"orderIds",所以下面foreach中collection="orderIds",如果未重命名则foreach中collection="list"
<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 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>
List<AnalysisInfo> listInfo(Long[] orderIds);
如果参数类型是Array则collection属性为array
<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 sales_order_id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> group by materials_id order by totalNum desc limit 5 </select>
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>
以上是“mybatis分割字符串并循环实现in多个参数的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。