您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在MyBatis中批量插入和刪除中雙層循環(huán),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
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=";"。
(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)的可以借鑒一下,有什么意見希望大家提出來。
此外。在使用過程中如果想讓查詢出的多條記錄變成一個(gè)Map,想直接通過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語句
使用這個(gè)insert語句可以在表名,字段名稱和字段個(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語句中都有用到。
第一層循環(huán)遍歷List,其中存放的是一條條要插入數(shù)據(jù)庫的數(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 是通過字符串拼接得到的。形如: 字段1,字段2,字段3
關(guān)于怎么在MyBatis中批量插入和刪除中雙層循環(huán)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。