广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis分割字符串并循环,实现in多个参数的操作
  • 936
分享到

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

2024-04-02 19:04:59 936人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

mybatis分割字符串并循环,实现in多个参数 mybatis xml代码: <select id="selectInXh" resultMap="Base

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)

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

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


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>

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


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>

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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

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

本文链接: https://www.lsjlt.com/news/127952.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作