MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例
本博客主要用兩個(gè)例子來(lái)說(shuō)明一下批量刪除和批量插入雙層循環(huán)的用法,順便自己記錄一下,方便以后使用。
1、批量刪除
(1):dao中的寫法:
public int batchDelPrice(@Param("deleteList")List<Map<String, Object>> deleteList);
其中deleteList是一個(gè)Map的集合,Map中的Object是一個(gè)list集合,deleteList拼接如下:
List<String> deletePriceId = getDelPriceId(oriPriceId,nowPriceId); Map<String,Object> deleteMap = new HashMap<String,Object>(); deleteMap.put("userCode", userCode); deleteMap.put("delete", deletePriceId); deleteList.add(deleteMap);
(2):xml中的寫法:
<delete id="batchDelPrice" parameterType="java.util.ArrayList"> <foreach collection="deleteList" item="deleteItem" separator=";"> delete from xxx where user_code = #{deleteItem.userCode} and product_id in <foreach collection="deleteItem.delete" item="item" index="index" open="(" close=")" separator=","> #{item} </foreach> </foreach> </delete>
注意:批量刪除操作,每個(gè)sql間是以分號(hào)間隔的,即最外層分隔符是separator=";"。
2、批量插入:
(1):dao中的寫法:
public int batchAddPrice(@Param("addList")List<Map<String, Object>> newAddList);
newAddList中的數(shù)據(jù)拼接如下:
List<Map<String,Object>> newAddList = new ArrayList<Map<String,Object>>(); for(int i = 0; i < addList.size(); i++){ Map<String,Object> userMap = addList.get(i); //獲取需要增加的產(chǎn)品id集合 List<String> priceIds = (List<String>) userMap.get("add"); List<Map<String,Object>> priceList = new ArrayList<Map<String,Object>>(); //遍歷產(chǎn)品id集合,取相應(yīng)的產(chǎn)品默認(rèn)價(jià)格,組裝成一個(gè)新的產(chǎn)品+默認(rèn)價(jià)格集合 for(int j = 0; j < priceIds.size(); j++){ Map<String,Object> priceMap = new HashMap<String,Object>(); String priceId = priceIds.get(j); //取相應(yīng)產(chǎn)品id的默認(rèn)價(jià)格 double defPrice = productDefPrice.get(Integer.valueOf(priceId)).get("default_price"); priceMap.put("priceId", priceId); priceMap.put("defPrice", defPrice); priceList.add(priceMap); } userMap.put("priceList", priceList); newAddList.add(userMap); }
(2):xml中的寫法:
<insert id="batchAddPrice" parameterType="java.util.ArrayList"> insert into xxx( user_code,product_id,price,efftime,index_num,pubtime )values <foreach collection="addList" item="addItem" separator="," > <foreach collection="addItem.priceList" item="priceItem" index="priceIndex" separator=","> ( #{addItem.userCode},#{priceItem.priceId},#{priceItem.defPrice},now(),1,now() ) </foreach> </foreach> </insert>
以上是批量插入和批量刪除的其中一種寫法,有用到雙層循環(huán)的可以借鑒一下,有什么意見(jiàn)希望大家提出來(lái)。
此外。在使用過(guò)程中如果想讓查詢出的多條記錄變成一個(gè)Map,想直接通過(guò)map.get(key)方式獲取value的同學(xué),可以參考下面的一個(gè)寫法:
(1):dao中的寫法:
@MapKey("product_id") public Map<Integer,Map<Integer,Double>> queryProductDefPrice();
其中@MapKey中是你要當(dāng)成查出的map的key值的字段名稱,Map<Integer,Double>中的字段為查詢出的字段,我這里查出的Map是:{1={product_id=1,default_price=10.0}}
(2):xml中的寫法:
<select id="queryProductDefPrice" resultType="java.util.HashMap"> select product_id,default_price from xxx </select>
希望對(duì)大家有所幫助。
補(bǔ)充:mybatis 兩層循環(huán)的insert語(yǔ)句
使用這個(gè)insert語(yǔ)句可以在表名,字段名稱和字段個(gè)數(shù)都不確定的情況下插入數(shù)據(jù)。
<insert id="insertOneSheet" parameterType="map"> <foreach collection = "lineList" item ="item" index ="index"> insert into ${table}(${lineColumn}) values (<foreach collection="item" index ="key" item="_value" separator=","> #{_value} </foreach>) </foreach> </insert>
這個(gè)insert接收一個(gè)map)作為參數(shù),其中放了,一個(gè)List,兩個(gè)字符串。其中字符串分別是table(要插入的表名),lineColumn(列名)這在insert語(yǔ)句中都有用到。
第一層循環(huán)遍歷List,其中存放的是一條條要插入數(shù)據(jù)庫(kù)的數(shù)據(jù),其中每條數(shù)據(jù)保存在一個(gè)map中。
第二層循環(huán)每次遍歷一個(gè)從List中取出的map。
區(qū)分兩個(gè)map,一個(gè)是作為參數(shù)的map,paramMap,里面有表名,字段名,LIst,一個(gè)是存放一行數(shù)據(jù)的map,dataMap
lineConlumn 是通過(guò)字符串拼接得到的。形如: 字段1,字段2,字段3
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Hystrix?Dashboard斷路監(jiān)控儀表盤的實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了Hystrix?Dashboard斷路監(jiān)控儀表盤的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09spring-boot react如何一步一步實(shí)現(xiàn)增刪改查
這篇文章主要介紹了spring-boot react如何一步一步實(shí)現(xiàn)增刪改查,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11JAVA中 Spring定時(shí)器的兩種實(shí)現(xiàn)方式
本文向您介紹Spring定時(shí)器的兩種實(shí)現(xiàn)方式,包括Java Timer定時(shí)和Quartz定時(shí)器,兩種Spring定時(shí)器的實(shí)現(xiàn)方式各有優(yōu)點(diǎn),可結(jié)合具體項(xiàng)目考慮是否采用。2015-09-09使用Java7的Files工具類和Path接口來(lái)訪問(wèn)文件的方法
下面小編就為大家分享一篇使用Java7的Files工具類和Path接口來(lái)訪問(wèn)文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Spring創(chuàng)建Bean的過(guò)程Debug的詳細(xì)流程
這篇文章主要介紹了Spring創(chuàng)建Bean的過(guò)程Debug的流程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Java編程實(shí)現(xiàn)月食簡(jiǎn)單代碼分享
這篇文章主要介紹了Java編程實(shí)現(xiàn)月食簡(jiǎn)單代碼分享,程序很簡(jiǎn)單,喜歡的朋友可以拿過(guò)去玩玩。2017-11-11springboot mybatis手動(dòng)事務(wù)的實(shí)現(xiàn)
本文主要介紹了springboot mybatis手動(dòng)事務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Java socket通訊實(shí)現(xiàn)過(guò)程及問(wèn)題解決
這篇文章主要介紹了Java socket通訊實(shí)現(xiàn)過(guò)程及問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01