【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 ...
随机推荐
- 设计模式之笔记--抽象工厂模式(Abstract Factory)
抽象工厂模式(Abstract Factory) 定义 抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图 描述 多个抽象产品 ...
- 一个大div里面包裹一个小div,里面的小div的点击事件不触发外面的这个大div的点击事件
一开始上html代码 <div id="div1" style="background: blue;width: 100px; height: 100px;&quo ...
- office2007/2010/2013输入公式的正确方式
博客中的文章均为 meelo 原创,请务必以链接形式注明本文地址 理工科的学生,写报告.写论文那面需要输入公式,过去大家常用的公式编辑器是mathtype,虽然功能强大,但输入极为不方便,输入个指数. ...
- Java学习笔记(三)——静态导入,package-info,Fall-through
[前面的话] 算是真正的放松了好几天时间,没有看任何书,没有任何任务,今天是过完年后的第一天上班时间,我又开始了我的学习之路,感觉还没有老,怎么心态越来越平静了,进入工作状态,就好好努力工作,新的一年 ...
- window 下 nginx+php+fastcgi 架设备忘
1.配置Php.ini 1)extension_dir = "./ext" 修改这个路径为真实的php的ext路径 2);extension=php_mysql.dll ;exte ...
- css3翻书效果
强大的css3不需要解释,代码分层理解[直接复制],很有意思. 效果图: <ul class="align"> <li> <figure class= ...
- 使用css做图标
首先原理是: 请一步一步粘贴代码,慢慢品味.其实,很简单... 1.首先三角形的前身是一个普通的矩形-正方形||长方形?ok! <div class='box'></div> ...
- PL/SQL 11.6 注册码
PL/SQL Developer 下载地址:Download PL/SQL Developer 11.0.6 注册码 Product Code:4t46t6vydkvsxekkvf3fjnpzy5wb ...
- 解析 Qt 程序在Windows 下发布
原文请看:http://www.cnblogs.com/elect-fans/archive/2012/03/15/2408579.html Qt 程序在Windows下发布是本文要介绍的内容,不多说 ...
- 设置iframe 载入页面的效果跟直接打开这个页面一样
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...