• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mybatis 搞代码 4年前 (2022-01-09) 16次浏览 已收录 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的同学,可以参考下面

本文来源gaodai.ma#com搞##代!^码@网3

的一个写法:

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MyBatis 实现批量插入和删除中双层循环的写法案例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址