欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis多个区间处理方式(双foreach循环)

程序员文章站 2022-06-09 19:51:02
目录mybatis多个区间处理思路多个foreach同时使用问题应用场景注意事项解决方案mybatis多个区间处理如图:要实现车辆数不同区间查询条件思路a.前端传数组,数组里面放"1-5&q...

mybatis多个区间处理

如图:要实现车辆数不同区间查询条件

mybatis多个区间处理方式(双foreach循环)

思路

a.前端传数组,数组里面放"1-5"string类型值            

b.后端mybatis用双foreach循环解析    

后端代码如下:

  <!--图例车辆数区间-->

<if test="countcargolist != null and countcargolist.size>0" >
          and (
          <foreach item="item" index="index" collection="countcargolist">
              (
            <foreach item="item2" index="index2" collection="item.split('-')">
                <if test="index2%2==0">
                    sfi.count_cargo >= #{item2}
                </if>
                <if test="index2%2==1">
                    and sfi.count_cargo <= #{item2}
                </if>
            </foreach>
              )
              <!--最后一次不用加or-->
              <if test="index != countcargolist.size-1" >
                  or
              </if>
          </foreach>
          )
      </if>

mybatis多个区间处理方式(双foreach循环)

mybatis多个区间处理方式(双foreach循环)

多个foreach同时使用问题

应用场景

1、多个表的数据一起删除的时候,有的时候不会建立外键,但主表的关联 表很多的时候,可以直接利用mybatis 进行多表删除。

注意事项

mybatis 中多个foreach 循环,第一次循环的collection  看到的值是一个(数组、list、map、对象,由collection的配置主导)。

第二次foreach 是直接copy第一个foreach的对象值,这个时候会一直累加对象。

解决方案

数组为例

dao层去掉@param注解

void batchdeletesystransferregist(string rowdata[]);

mybatis

<delete id="batchdeletesystransferregist" parametertype="string">
        delete from sys_transfer_regist_item  where transfer_regist_id in
        <foreach item="rowdata" collection="array" open="(" separator="," close=")">
            #{rowdata}
        </foreach>
        ;delete from sys_transfer_regist_piece  where transfer_regist_id in
        <foreach item="rowdata1" collection="array" open="(" separator="," close=")">
            #{rowdata1}
        </foreach>
......
    </delete>

切记;隔开。

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