av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例

瀏覽:171日期:2023-10-20 13:06:53

本博客主要用兩個(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 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 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 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 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

MyBatis 實(shí)現(xiàn)批量插入和刪除中雙層循環(huán)的寫法案例

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章:
主站蜘蛛池模板: 亚洲一级二级三级 | 国产麻豆一区二区三区 | 国产视频在线看 | 97超碰在线免费观看 | 亚洲国产欧美日韩 | 亚洲欧美另类在线 | 黄免费网站| 在线成人免费视频 | a在线免费观看 | 五月天婷婷激情 | 国产一级网站 | 免费观看av | 国内福利视频 | 日韩在线不卡视频 | 国产精品一区二区久久 | 美女国产精品 | 欧美国产精品 | 青青青草视频 | www.欧美精品 | 五月婷婷网 | 青草视频网站 | 日韩小视频 | 香蕉视频国产 | 夜夜操夜夜操 | 欧美日韩一区二区三区视频 | 国产97视频 | 国产一区二区在线观看视频 | 久久久三级| 天天射影院 | 93久久精品日日躁夜夜躁欧美 | 午夜性视频 | 亚洲激情偷拍 | 高清一级片| 自拍偷拍中文字幕 | 亚洲黄色大片 | 97精品国产97久久久久久免费 | 性欧美69 | 久久久久久久久国产精品 | 69av视频| 免费看黄色av | 日日不卡av |