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

2 篇文章 0 订阅
2 篇文章 0 订阅

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

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 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>

注意:批量删除操作,每个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 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>

以上是批量插入和批量删除的其中一种写法,有用到双层循环的可以借鉴一下,有什么意见希望大家提出来。

此外。在使用过程中如果想让查询出的多条记录变成一个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 id="queryProductDefPrice" resultType="java.util.HashMap">
	select product_id,default_price from xxx
</select>
希望对大家有所帮助。
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值