这篇文章将为大家详细讲解有关怎么在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 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=";"。
(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>
希望对大家有所帮助。
补充:mybatis 两层循环的insert语句
使用这个insert语句可以在表名,字段名称和字段个数都不确定的情况下插入数据。
<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>
这个insert接收一个map)作为参数,其中放了,一个List,两个字符串。其中字符串分别是table(要插入的表名),lineColumn(列名)这在insert语句中都有用到。
第一层循环遍历List,其中存放的是一条条要插入数据库的数据,其中每条数据保存在一个map中。
第二层循环每次遍历一个从List中取出的map。
区分两个map,一个是作为参数的map,paramMap,里面有表名,字段名,LIst,一个是存放一行数据的map,dataMap
lineConlumn 是通过字符串拼接得到的。形如: 字段1,字段2,字段3
关于怎么在MyBatis中批量插入和删除中双层循环就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。