【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 ...
随机推荐
- cf786a
title: CodeForces 786A Berzerk data: 2018-3-3 10:29:40 tags: 博弈论 bfs 无限 with draw copyright: true ca ...
- Java中的原子操作类
转载: <ava并发编程的艺术>第7章 当程序更新一个变量时,如果多线程同时更新这个变量,可能得到期望之外的值,比如变量i=1,A线程更新i+1,B线程也更新i+1,经过两个线程操作之后可 ...
- EL表达式使用时出现NumberFormatException异常
从后端数据库取出书本集合,然后循环输出到前端表格: <c:forEach items="${bookManagedBean.bookList}" var="book ...
- redis之(十)redis实现消息中间件的功能
[一]任务队列的好处 --->松耦合:生产者和消费者无需知道彼此实现的细节,只需要约定好任务的描述格式.这使得生产者和消费者可以由不同的团队使用不同的编程语言编写. --->易于扩展:消费 ...
- Letter Combinations of a Phone Number——简单的回溯算法
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- linux 下 php 扩展
首先查看,php 原来安装的配置 /www/server/php/72/bin/php -i | grep configure (php 位置) 加上你的扩展 '--with-mcrypt' make ...
- Struts2 简单的上传文件并且显示图片
代码结构: UploadAction.java package com.action; import java.io.File; import java.io.FileInputStream; imp ...
- POJ 1860 Currency Exchange【SPFA判环】
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- 基于BeanNameViewResolver解析器,自定义视图
概述 基于spring-mvc自定义视图,以BeanNameViewResolver作为解析器,以满足特殊需求. 本文以输出多个pdf文件的压缩文件,供前台下载的需求为例:但是不提供服务层实现. 实现 ...
- 使用phonegap开发安卓HLS播放软件解决方案
目前使用phonegap开发的手机应用,很少涉及视频播放的功能,究其原因,主要是phonegap提供的API里面对视频播放功能支持度不够,当然播放音频一般情况下还是能够实现的,由于工作需要,自己研究了 ...