mybatis多個區(qū)間處理方式(雙foreach循環(huán))
mybatis多個區(qū)間處理
如圖:要實現(xiàn)車輛數(shù)不同區(qū)間查詢條件

思路
a.前端傳數(shù)組,數(shù)組里面放"1-5"String類型值
b.后端mybatis用雙foreach循環(huán)解析
后端代碼如下:
<!--圖例車輛數(shù)區(qū)間-->
<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>

多個foreach同時使用問題
應(yīng)用場景
1、多個表的數(shù)據(jù)一起刪除的時候,有的時候不會建立外鍵,但主表的關(guān)聯(lián) 表很多的時候,可以直接利用mybatis 進行多表刪除。
注意事項
mybatis 中多個foreach 循環(huán),第一次循環(huán)的collection 看到的值是一個(數(shù)組、list、map、對象,由collection的配置主導(dǎo))。
第二次foreach 是直接copy第一個foreach的對象值,這個時候會一直累加對象。
解決方案
數(shù)組為例
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>切記;隔開。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java對象轉(zhuǎn)JSON時動態(tài)的增刪改查屬性詳解
這篇文章主要介紹了Java對象轉(zhuǎn)JSON時如何動態(tài)的增刪改查屬性的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring Boot整合mybatis使用注解實現(xiàn)動態(tài)Sql、參數(shù)傳遞等常用操作(實現(xiàn)方法)
這篇文章主要介紹了Spring Boot整合mybatis使用注解實現(xiàn)動態(tài)Sql、參數(shù)傳遞等常用操作(實現(xiàn)方法),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

