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

分别展示 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就不会打印出来了。!!!!

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

最新文章

  1. javascript数据结构与算法---检索算法
  2. 以小时候玩的贪吃蛇为例,对于Java图像界面的学习感悟
  3. android 调用地图
  4. oc TableView 分割线(separator)部分显示问题
  5. mysql-4 数据检索(2)
  6. C#~异步编程续~.net4.5主推的await&amp;async应用(转)
  7. SQL Server2008创建约束图解
  8. SQL SERVER定时任务执行跟踪--供远程查看 [原创]
  9. Spring AOP Example – Advice
  10. Notes里OK,CANCEL按钮的设定
  11. pytho字符串处理内置方法一览表
  12. 【原创】Linux基础之文件编码
  13. windows MYSQL 安装及修改root密码
  14. 洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)
  15. Codeforces 670F - Restore a Number - [字符串]
  16. timeStamp(时间戳) 事件属性
  17. POJ3104--Drying(Binary Search)
  18. ubuntu ssh 连接加速
  19. Element ui 中使用table组件实现分页记忆选中
  20. java 循环时候当达到这个类型的极值时 会停止输出

热门文章

  1. XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件
  2. 关于c++的string的operator =
  3. 设计模式之笔记--建造者模式(Builder)
  4. Scrapy爬虫:抓取大量斗图网站最新表情图片
  5. Jquery和JS实现浏览器全屏
  6. js上传文件(图片)限制格式及大小为3M
  7. Hive分组取第一条记录
  8. win10网速慢
  9. Grid++Report 注册
  10. f&#39;lask源码