您的位置:首页技术文章
文章详情页

MyBatis 实现批量插入和删除中双层循环的写法案例

【字号: 日期:2023-10-20 13:06:53浏览:25作者:猪猪
导读:本博客主要用两个例子来说明一下批量删除和批量插入双层循环的用法,顺便自己记录一下,方便以后使用。1、批量删除(1):dao中的写法:public int batchDelPrice(@Param('deleteList')List<Map<String, Object>> d...

本博客主要用两个例子来说明一下批量删除和批量插入双层循环的用法,顺便自己记录一下,方便以后使用。

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

MyBatis 实现批量插入和删除中双层循环的写法案例

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Mybatis 数据库
相关文章: