【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作
==================================================================
分别展示 mybatis 批量新增 和 批量更新 的操作:
controller层:
- goodsService.batchInsert(insertGoodsList);
- goodsService.batchUpdate(updateGoodsList);
service层:
切割List的方法【https://www.cnblogs.com/sxdcgaq8080/p/9376947.html】【建议分批次处理,每次处理1000条】【实际根据每条数据的大小,自行划分】
- @Override
- @Transactional
- public int batchInsert(List<Goods> list) {
- int a = 0;
- List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
- for (List<Goods> goodsList : goodsAllList) {
- a += mapper.batchInsert(goodsList);
- }
- return a;
- }
- @Override
- @Transactional
- public int batchUpdate(List<Goods> list) {
- int a = 0;
- List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
- Map<String,Object> map = new HashMap<>();
- for (List<Goods> goodsList : goodsAllList) {
- map.put("list",goodsList);
- a += mapper.batchUpdate(map);
- }
- return a;
- }
Mapper.java层
- int batchInsert(List<Goods> list);
- int batchUpdate(Map<String,Object> map);
Mapper.xml层
【注意,batchUpdate的原理,是循环拼接sql,一次连接数据库,执行多条update语句】
- <insert id="batchInsert">
- INSERT INTO goods
- (create_date,update_date,create_id,update_id,enabled,
- tenement_id,uid,name,py_all,py_head,
- outer_id,outer_code,mnemonic_code,del_flag,enabled_flag,
- goods_type_uid,url,bar_cide,sale_price,integral,
- scan_name,brand_uid,en_name)
- VALUES
- <foreach collection="list" item="item" separator=",">
- (#{item.createDate},#{item.updateDate},#{item.createId},#{item.updateId},#{item.enabled},
- #{item.tenementId},#{item.uid},#{item.name},#{item.pyAll},#{item.pyHead},
- #{item.outerId},#{item.outerCode},#{item.mnemonicCode},#{item.delFlag},#{item.enabledFlag},
- #{item.goodsTypeUid},#{item.url},#{item.barCide},#{item.salePrice},#{item.integral},
- #{item.scanName},#{item.brandUid},#{item.enName})
- </foreach>
- </insert>
- <update id="batchUpdate" parameterType="java.util.Map">
- <foreach collection="list" separator=";" item="goods">
- update
- goods
- SET
- update_date = #{goods.updateDate},
- update_id = #{goods.updateId},
- enabled = #{goods.enabled},
- name = #{goods.name},
- py_all = #{goods.pyAll},
- py_head = #{goods.pyHead},
- outer_code = #{goods.outerCode},
- mnemonic_code = #{goods.mnemonicCode},
- del_flag = #{goods.delFlag},
- enabled_flag = #{goods.enabledFlag},
- goods_type_uid = #{goods.goodsTypeUid},
- url = #{goods.url},
- bar_cide = #{goods.barCide},
- sale_price = #{goods.salePrice},
- integral = #{goods.integral},
- scan_name = #{goods.scanName},
- brand_uid = #{goods.brandUid},
- en_name = #{goods.enName}
- where
- outer_id = #{goods.outerId}
- and
- tenement_id = #{goods.tenementId}
- </foreach>
- </update>
最后如果,批量插入可以成功,但是批量更新失败,可以参考:https://www.cnblogs.com/sxdcgaq8080/p/10565023.html
最后需要注意的是,如果解决了批量更新问题后,还想按照【https://www.cnblogs.com/sxdcgaq8080/p/9100178.html】打印sql,就失效了,sql就不会打印出来了。!!!!
===========================================================================
【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作的更多相关文章
- mybatis中批量插入以及更新
1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...
- mybatis 注解的方式批量插入,更新数据
一,当向数据表中插入一条数据时,一般先检查该数据是否已经存在,如果存在更新,不存在则新增 使用关键字 ON DUPLICATE KEY UPDATE zk_device_id为主键 model ...
- Oracle+Mybatis批量插入,更新和删除
1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...
- java批量插入或更新的问题
在批量插入或者更新中,setXXX的时候字段类型必须一致.例如:在普通sql中 pstmt8.setBigDecimal(j ,xxx);可以写成pstmt8.setString(j,xxx.toSt ...
- C#使用SqlDataAdapter 实现数据的批量插入和更新
近日由于项目要求在需要实现中型数据的批量插入和更新,晚上无聊,在网上看到看到这样的一个实现方法,特摘抄过来,以便以后可能用到参考. 一.数据的插入 DateTime begin = DateTime. ...
- MySQL on duplicate key update 批量插入并更新已存在数据
业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...
- Python中elasticsearch插入和更新数据的实现方法
Python中elasticsearch插入和更新数据的实现方法 这篇文章主要介绍了Python中elasticsearch插入和更新数据的实现方法,需要的朋友可以参考下 首先,我的索引结构是酱紫的. ...
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- Mybatis中实现oracle的批量插入、更新
oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...
随机推荐
- ORACLE导入Excel数据
首先建好一个和Excel表字段对应字段的表,然后 select t.* from 表名 t for update; 点击这个锁子,打开它 粘贴,然后 再提交事务即可
- tornado write render redirect IP
write 用法( self.flush() ) render (跳转指定网页)用法 redirect(跳转指定路由)用法 self.request.remote_ip 显示用户 IP 地址 less ...
- 在 Visual Studio 中使用正则表达式
Visual Studio 使用 .NET framework 正则表达式查找和替换文本. 在 Visual Studio 2010 和早期版本中,Visual Studio 在“查找和替换”窗口中使 ...
- OpenCV利用矩阵实现图像旋转
利用OpenCV的矩阵操作实现图像的逆时针旋转90度操作 代码 Mat src = imread("C:\\Users\\fenggl\\Desktop\\测试.jpg",MREA ...
- HDU 3045 picnic cows(斜率DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 题目大意:有n个数,可以把n个数分成若干组,每组不得小于m个数,每组的价值=除了该组最小值以外每 ...
- 基于UDP套接字编程实例
data.h #ifndef DATA_H #define DATA_H #include <stdio.h> #include <string.h> #include < ...
- 通过第三方组件NPOI读取Excel的方法
public class ExcelHelper { public class x2003 { #region Excel2003 /// <summary> /// 将Excel文件中的 ...
- Mybatis框架-2
1.Mybatis中的接口形式 在Mybatis中使用接口形式将通过代理对象调用方法,从而实现sql的执行 1)定义一个接口 package mapper; import java.util.List ...
- getAllResponseHeaders() 必须放到onload里面
<html><head> <meta charset="utf-8"> <title>test</title> < ...
- 一:Ionic Framework初体验
因项目关系,需要开发一个平板使用的应用程序,刚开始以为需要使用Andriod,后来经理提供了一个解决方案,Ionic Framework https://ionicframework.com/ 第一步 ...