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>

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

相关文章

  • 初次体验MyBatis的注意事项

    初次体验MyBatis的注意事项

    今天给大家带来的是关于MyBatis的相关知识,文章围绕着MyBatis的用法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Flutter实现文本组件、图标及按钮组件的代码

    Flutter实现文本组件、图标及按钮组件的代码

    这篇文章主要介绍了Flutter实现文本组件、图标及按钮组件的代码,本文给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2019-07-07
  • Spring Cloud Config分布式配置中心使用介绍详解

    Spring Cloud Config分布式配置中心使用介绍详解

    分布式配置中心应用场景 往往,我们使用配置文件管理⼀些配置信息,比如application.yml 单体应用架构:配置信息的管理、维护并不会显得特别麻烦,手动操作就可以,因为就一个工程
    2022-09-09
  • Java微服务的打包问题解决

    Java微服务的打包问题解决

    本文主要介绍了Java微服务的打包问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 解决idea默认带的equals和hashcode引起的bug

    解决idea默认带的equals和hashcode引起的bug

    这篇文章主要介绍了解决idea默认带的equals和hashcode引起的bug,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 在SpringBoot中使用@Value注解来设置默认值的方法

    在SpringBoot中使用@Value注解来设置默认值的方法

    Spring Boot提供了一种使用注解设置默认值的方式,即使用 @Value 注解,下面这篇文章主要给大家介绍了关于如何在SpringBoot中使用@Value注解来设置默认值的相关资料,需要的朋友可以参考下
    2023-10-10
  • Java如何基于ProcessBuilder类调用外部程序

    Java如何基于ProcessBuilder类调用外部程序

    这篇文章主要介绍了Java如何基于ProcessBuilder类调用外部程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • java使用PDFRenderer实现预览PDF功能

    java使用PDFRenderer实现预览PDF功能

    这篇文章主要为大家详细介绍了java使用PDFRenderer实现预览PDF功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • 浅谈Java中spring 线程异步执行

    浅谈Java中spring 线程异步执行

    这篇文章主要介绍了浅谈spring 线程异步执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • java输入多个数据(不确定),排序,并求最大值的方法

    java输入多个数据(不确定),排序,并求最大值的方法

    今天小编就为大家分享一篇java输入多个数据(不确定),排序,并求最大值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07

最新评论