MyBatis 實現批量插入和刪除中雙層循環的寫法案例
本博客主要用兩個例子來說明一下批量刪除和批量插入雙層循環的用法,順便自己記錄一下,方便以后使用。
1、批量刪除(1):dao中的寫法:
public int batchDelPrice(@Param('deleteList')List<Map<String, Object>> deleteList);
其中deleteList是一個Map的集合,Map中的Object是一個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>
注意:批量刪除操作,每個sql間是以分號間隔的,即最外層分隔符是separator=';'。
2、批量插入:(1):dao中的寫法:
public int batchAddPrice(@Param('addList')List<Map<String, Object>> newAddList);
newAddList中的數據拼接如下:
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); //獲取需要增加的產品id集合 List<String> priceIds = (List<String>) userMap.get('add'); List<Map<String,Object>> priceList = new ArrayList<Map<String,Object>>(); //遍歷產品id集合,取相應的產品默認價格,組裝成一個新的產品+默認價格集合 for(int j = 0; j < priceIds.size(); j++){ Map<String,Object> priceMap = new HashMap<String,Object>(); String priceId = priceIds.get(j); //取相應產品id的默認價格 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>
以上是批量插入和批量刪除的其中一種寫法,有用到雙層循環的可以借鑒一下,有什么意見希望大家提出來。
此外。在使用過程中如果想讓查詢出的多條記錄變成一個Map,想直接通過map.get(key)方式獲取value的同學,可以參考下面的一個寫法:
(1):dao中的寫法:
@MapKey('product_id') public Map<Integer,Map<Integer,Double>> queryProductDefPrice();
其中@MapKey中是你要當成查出的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>
希望對大家有所幫助。
補充:mybatis 兩層循環的insert語句
使用這個insert語句可以在表名,字段名稱和字段個數都不確定的情況下插入數據。
<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>
這個insert接收一個map)作為參數,其中放了,一個List,兩個字符串。其中字符串分別是table(要插入的表名),lineColumn(列名)這在insert語句中都有用到。
第一層循環遍歷List,其中存放的是一條條要插入數據庫的數據,其中每條數據保存在一個map中。
第二層循環每次遍歷一個從List中取出的map。
區分兩個map,一個是作為參數的map,paramMap,里面有表名,字段名,LIst,一個是存放一行數據的map,dataMap
lineConlumn 是通過字符串拼接得到的。形如: 字段1,字段2,字段3
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章:
