mybatis分割字符串并循环实现in多个参数的示例分析

发布时间:2021-06-10 10:30:00 作者:小新
来源:亿速云 阅读:541

这篇文章主要为大家展示了“mybatis分割字符串并循环实现in多个参数的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“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分割字符串并循环实现in多个参数的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Mybatis如何传入多个参数
  2. Python中一个for循环循环多个变量的示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis 字符串

上一篇:如何使用HBuilder打包APP

下一篇:怎么让java只根据数据库表名自动生成实体类

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》