【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 ...
随机推荐
- 计数排序的实现--适用于元素均较小的seq
今天无聊就打算把所有的排序算法都看一遍... 计数排序的时间复杂度是O(n),在算法导论中,用决策树模型中论证了,比较排序的情况为nlogn的复杂度.而计数排序的时间复杂度小于他的原因就是它不需要进行 ...
- SVN使用详解
一.SVN的使用 项目经理使用,写好项目框架.文档等. 李四(程序员)的使用,在项目经理写好的框架上进行开发. 二.SVN三大指令 Checkout(检出操作): 连接到svn服务器 更新服务器数据到 ...
- 大小端 Big-Endian 与 Little-Endian
应该说没做底层开发(硬件或驱动)的人很可能不会彻底理解大小端的概念,大小端不是简单的一句“大端在前”还是“小端在前”能够概括的问题.在cpu, 内存, 操作系统, 编译选项, 文件,网络传输中均有大小 ...
- Java之CyclicBarrier使用
http://blog.csdn.net/shihuacai/article/details/8856407 1.类说明: 一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (commo ...
- Jump Game I&&II——入门级贪心算法
Jump Game I Given an array of non-negative integers, you are initially positioned at the first index ...
- Python图像处理库(2)
1.4 SciPy SciPy(http://scipy.org/) 是建立在 NumPy 基础上,用于数值运算的开源工具包.SciPy 提供很多高效的操作,可以实现数值积分.优化.统计.信号处理,以 ...
- WordPress主循环(The Loop)函数have_posts(),the_post()详解
WordPress中调用文章标题是the_title();调用文章内容时用到the_content();调用文章的作者时用到the_author();等等这些函数,都需要在主循环中使用,下面就介绍一下 ...
- WebDriver自动化测试工具(2)---基本操作
一.设置打开的浏览器大小/位置 driver.Manage().Window.Maximize(); //最大化 driver.Manage().Window.Position = , ); //设置 ...
- windows下rabbitmq(架构师必备神器)集群搭建
准备2台机器,例如:computera: 10.0.0.151 computerb:10.0.0.234 都安装erlang环境和rabbitmq服务,注意otp环境和rabbitmq服务必须版 ...
- CentOS7.5安裝配置多协议下载器Aria2
一.搭建 Aria2 以及 AriaNg Web UI 使用Docker构建的Aria2 参考 aria2-ariang-docker 以及 aria2-ariang-x-docker-compose ...