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

 更新时间:2021年06月10日 08:37:57   作者:思想永无止境  
这篇文章主要介绍了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)

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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 基于Java实现获取本地IP地址和主机名

    基于Java实现获取本地IP地址和主机名

    这篇文章主要介绍了基于Java实现获取本地IP地址和主机名,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • java并发编程专题(九)----(JUC)浅析CyclicBarrier

    java并发编程专题(九)----(JUC)浅析CyclicBarrier

    这篇文章主要介绍了java CyclicBarrier的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • rabbitmq消息ACK确认机制及发送失败处理方式

    rabbitmq消息ACK确认机制及发送失败处理方式

    这篇文章主要介绍了rabbitmq消息ACK确认机制及发送失败处理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java中JSONObject和Map<String, Object>的转换方法

    Java中JSONObject和Map<String, Object>的转换方法

    平时对接口时,经常遇到json字符串和map对象之间的交互,这篇文章主要给大家介绍了关于Java中JSONObject和Map<String, Object>的转换方法,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • IntelliJ idea 如何生成动态的JSON字符串(步骤详解)

    IntelliJ idea 如何生成动态的JSON字符串(步骤详解)

    这篇文章主要介绍了IntelliJ idea 如何生成动态的JSON字符串,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java CountDownLatch的源码硬核解析

    Java CountDownLatch的源码硬核解析

    对于并发执行,Java中的CountDownLatch是一个重要的类。为了更好的理解CountDownLatch这个类,本文将通过例子和源码带领大家深入解析这个类的原理,感兴趣的可以学习一下
    2022-10-10
  • Java获取年月日(格式:xxxx年xx月xx日)的方法详解

    Java获取年月日(格式:xxxx年xx月xx日)的方法详解

    在开发应用程序时,经常需要获取当前的年、月、日,并以特定格式进行展示或处理,本文将介绍如何获取年月日,并将其格式化为“xxxx年xx月xx日”的形式,帮助你在应用程序中处理日期信息,需要的朋友可以参考下
    2023-10-10
  • Java底层基于二叉搜索树实现集合和映射/集合Set功能详解

    Java底层基于二叉搜索树实现集合和映射/集合Set功能详解

    这篇文章主要介绍了Java底层基于二叉搜索树实现集合和映射/集合Set功能,结合实例形式分析了Java使用二叉搜索树实现集合和映射相关操作技巧,需要的朋友可以参考下
    2020-03-03
  • java @Data布尔值boolean的坑及解决

    java @Data布尔值boolean的坑及解决

    本文介绍了在使用Spring框架时,遇到的一个属性命名规则问题,在Spring框架中,如果类的属性名称第一个字母小写,第二个字母大写,那么在调用set方法时,Spring会将属性的后面的字母转换为小写,这种情况下,如果下游消费端调用得到的返回json串
    2024-10-10
  • Java的字符读写类CharArrayReader和CharArrayWriter使用示例

    Java的字符读写类CharArrayReader和CharArrayWriter使用示例

    这篇文章主要介绍了Java的字符读写类CharArrayReader和CharArrayWriter使用示例,两个类分别继承于Reader和Writer,需要的朋友可以参考下
    2016-06-06

最新评论