==================================================================

分别展示 mybatis 批量新增  和 批量更新   的操作:

controller层:

  1. goodsService.batchInsert(insertGoodsList);
  2.  
  3. goodsService.batchUpdate(updateGoodsList);

service层:

切割List的方法【https://www.cnblogs.com/sxdcgaq8080/p/9376947.html】【建议分批次处理,每次处理1000条】【实际根据每条数据的大小,自行划分】

  1.   @Override
  2. @Transactional
  3. public int batchInsert(List<Goods> list) {
  4. int a = 0;
  5.  
  6. List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
  7. for (List<Goods> goodsList : goodsAllList) {
  8. a += mapper.batchInsert(goodsList);
  9. }
  10.  
  11. return a;
  12. }
  13.  
  14. @Override
  15. @Transactional
  16. public int batchUpdate(List<Goods> list) {
  17. int a = 0;
  18.  
  19. List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
  20. Map<String,Object> map = new HashMap<>();
  21. for (List<Goods> goodsList : goodsAllList) {
  22. map.put("list",goodsList);
  23. a += mapper.batchUpdate(map);
  24. }
  25.  
  26. return a;
  27. }

Mapper.java层

  1. int batchInsert(List<Goods> list);
  2.  
  3. int batchUpdate(Map<String,Object> map);

Mapper.xml层

【注意,batchUpdate的原理,是循环拼接sql,一次连接数据库,执行多条update语句】

  1. <insert id="batchInsert">
  2. INSERT INTO goods
  3.  
  4. (create_date,update_date,create_id,update_id,enabled,
  5. tenement_id,uid,name,py_all,py_head,
  6. outer_id,outer_code,mnemonic_code,del_flag,enabled_flag,
  7. goods_type_uid,url,bar_cide,sale_price,integral,
  8. scan_name,brand_uid,en_name)
  9.  
  10. VALUES
  11.  
  12. <foreach collection="list" item="item" separator=",">
  13. (#{item.createDate},#{item.updateDate},#{item.createId},#{item.updateId},#{item.enabled},
  14. #{item.tenementId},#{item.uid},#{item.name},#{item.pyAll},#{item.pyHead},
  15. #{item.outerId},#{item.outerCode},#{item.mnemonicCode},#{item.delFlag},#{item.enabledFlag},
  16. #{item.goodsTypeUid},#{item.url},#{item.barCide},#{item.salePrice},#{item.integral},
  17. #{item.scanName},#{item.brandUid},#{item.enName})
  18. </foreach>
  19. </insert>
  20.  
  21. <update id="batchUpdate" parameterType="java.util.Map">
  22. <foreach collection="list" separator=";" item="goods">
  23. update
  24. goods
  25. SET
  26. update_date = #{goods.updateDate},
  27. update_id = #{goods.updateId},
  28. enabled = #{goods.enabled},
  29. name = #{goods.name},
  30. py_all = #{goods.pyAll},
  31. py_head = #{goods.pyHead},
  32. outer_code = #{goods.outerCode},
  33. mnemonic_code = #{goods.mnemonicCode},
  34. del_flag = #{goods.delFlag},
  35. enabled_flag = #{goods.enabledFlag},
  36. goods_type_uid = #{goods.goodsTypeUid},
  37. url = #{goods.url},
  38. bar_cide = #{goods.barCide},
  39. sale_price = #{goods.salePrice},
  40. integral = #{goods.integral},
  41. scan_name = #{goods.scanName},
  42. brand_uid = #{goods.brandUid},
  43. en_name = #{goods.enName}
  44. where
  45. outer_id = #{goods.outerId}
  46. and
  47. tenement_id = #{goods.tenementId}
  48.  
  49. </foreach>
  50. </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进行批量操作的更多相关文章

  1. mybatis中批量插入以及更新

    1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...

  2. mybatis 注解的方式批量插入,更新数据

    一,当向数据表中插入一条数据时,一般先检查该数据是否已经存在,如果存在更新,不存在则新增  使用关键字  ON DUPLICATE KEY UPDATE zk_device_id为主键 model  ...

  3. Oracle+Mybatis批量插入,更新和删除

    1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...

  4. java批量插入或更新的问题

    在批量插入或者更新中,setXXX的时候字段类型必须一致.例如:在普通sql中 pstmt8.setBigDecimal(j ,xxx);可以写成pstmt8.setString(j,xxx.toSt ...

  5. C#使用SqlDataAdapter 实现数据的批量插入和更新

    近日由于项目要求在需要实现中型数据的批量插入和更新,晚上无聊,在网上看到看到这样的一个实现方法,特摘抄过来,以便以后可能用到参考. 一.数据的插入 DateTime begin = DateTime. ...

  6. MySQL on duplicate key update 批量插入并更新已存在数据

    业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...

  7. Python中elasticsearch插入和更新数据的实现方法

    Python中elasticsearch插入和更新数据的实现方法 这篇文章主要介绍了Python中elasticsearch插入和更新数据的实现方法,需要的朋友可以参考下 首先,我的索引结构是酱紫的. ...

  8. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  9. Mybatis中实现oracle的批量插入、更新

    oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...

随机推荐

  1. ORACLE导入Excel数据

    首先建好一个和Excel表字段对应字段的表,然后 select t.* from 表名 t  for update; 点击这个锁子,打开它 粘贴,然后 再提交事务即可

  2. tornado write render redirect IP

    write 用法( self.flush() ) render (跳转指定网页)用法 redirect(跳转指定路由)用法 self.request.remote_ip 显示用户 IP 地址 less ...

  3. 在 Visual Studio 中使用正则表达式

    Visual Studio 使用 .NET framework 正则表达式查找和替换文本. 在 Visual Studio 2010 和早期版本中,Visual Studio 在“查找和替换”窗口中使 ...

  4. OpenCV利用矩阵实现图像旋转

    利用OpenCV的矩阵操作实现图像的逆时针旋转90度操作 代码 Mat src = imread("C:\\Users\\fenggl\\Desktop\\测试.jpg",MREA ...

  5. HDU 3045 picnic cows(斜率DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3045 题目大意:有n个数,可以把n个数分成若干组,每组不得小于m个数,每组的价值=除了该组最小值以外每 ...

  6. 基于UDP套接字编程实例

    data.h #ifndef DATA_H #define DATA_H #include <stdio.h> #include <string.h> #include < ...

  7. 通过第三方组件NPOI读取Excel的方法

    public class ExcelHelper { public class x2003 { #region Excel2003 /// <summary> /// 将Excel文件中的 ...

  8. Mybatis框架-2

    1.Mybatis中的接口形式 在Mybatis中使用接口形式将通过代理对象调用方法,从而实现sql的执行 1)定义一个接口 package mapper; import java.util.List ...

  9. getAllResponseHeaders() 必须放到onload里面

    <html><head> <meta charset="utf-8"> <title>test</title> < ...

  10. 一:Ionic Framework初体验

    因项目关系,需要开发一个平板使用的应用程序,刚开始以为需要使用Andriod,后来经理提供了一个解决方案,Ionic Framework https://ionicframework.com/ 第一步 ...