博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis 通过 BATCH 批量提交
阅读量:4324 次
发布时间:2019-06-06

本文共 4459 字,大约阅读时间需要 14 分钟。

本文由 转码, 原文地址 https://www.jb51.net/article/153382.htm

很多人在用 MyBatis 或者 通用 Mapper 时,经常会问有没有批量插入和批量更新的方法。

实际上许多时候没必要用<foreach> 去实现特别复杂的批量操作。直接通过 MyBatis 的 BATCH 方式执行增删改方法即可。

下面是一个批量用法的例子:

@Autowiredprivate SqlSessionFactory sqlSessionFactory;@Transactional(rollbackFor = Exception.class)@Overridepublic void batchTest() {  SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);  CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);  List countries = mapper.selectAll();  for (int i = 0; i < countries.size(); i++) {    Country country = countries.get(i);    country.setCountryname(country.getCountryname() + "Test");    mapper.updateByPrimaryKey(country);    //每 50 条提交一次    if((i + 1) % 50 == 0){      sqlSession.flushStatements();    }  }  sqlSession.flushStatements();}

在上面例子中,在Service中直接注入了SqlSessionFactory,通过下面方法获取了一个可以批量提交的SqlSession

SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

后续通过SqlSession直接执行方法,或者获取的Mapper接口,都使用的批量提交方式。

上述代码执行过程中输出的日志如下:

DEBUG - Creating new transaction with name [com.isea533.mybatis.service.impl.CountryServiceImpl.batchTest]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT

DEBUG - Acquired Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2] for JDBC transaction
DEBUG - Switching JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2] to manual commit
DEBUG - JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2] will be managed by Spring
DEBUG - ==>  Preparing: SELECT Id,countryname,countrycode FROM country
DEBUG - ==> Parameters:
DEBUG - <==      Total: 183
DEBUG - ==>  Preparing: UPDATE country SET Id = Id,countryname = ?,countrycode = ? WHERE Id = ?
DEBUG - ==> Parameters: AngolaTest(String), AO(String), 1(Integer)
DEBUG - ==> Parameters: AfghanistanTest(String), AF(String), 2(Integer)
DEBUG - ==> Parameters: AlbaniaTest(String), AL(String), 3(Integer)
==========================================
... 省略中间部分参数
==========================================
DEBUG - ==> Parameters: EthiopiaTest(String), ET(String), 50(Integer)
DEBUG - ==>  Preparing: UPDATE country SET Id = Id,countryname = ?,countrycode = ? WHERE Id = ?
DEBUG - ==> Parameters: FijiTest(String), FJ(String), 51(Integer)
DEBUG - ==> Parameters: FinlandTest(String), FI(String), 52(Integer)
==========================================
... 省略中间部分参数
==========================================
DEBUG - ==> Parameters: MadagascarTest(String), MG(String), 98(Integer)
DEBUG - ==> Parameters: MalawiTest(String), MW(String), 99(Integer)
DEBUG - ==> Parameters: MalaysiaTest(String), MY(String), 100(Integer)
DEBUG - ==>  Preparing: UPDATE country SET Id = Id,countryname = ?,countrycode = ? WHERE Id = ?
DEBUG - ==> Parameters: MaldivesTest(String), MV(String), 101(Integer)
DEBUG - ==> Parameters: MaliTest(String), ML(String), 102(Integer)
==========================================
... 省略中间部分参数
==========================================
DEBUG - ==> Parameters: South AfricaTest(String), ZA(String), 149(Integer)
DEBUG - ==> Parameters: SpainTest(String), ES(String), 150(Integer)
DEBUG - ==>  Preparing: UPDATE country SET Id = Id,countryname = ?,countrycode = ? WHERE Id = ?
DEBUG - ==> Parameters: Sri LankaTest(String), LK(String), 151(Integer)
DEBUG - ==> Parameters: St.LuciaTest(String), LC(String), 152(Integer)
==========================================
... 省略中间部分参数
==========================================
DEBUG - ==> Parameters: ZaireTest(String), ZR(String), 182(Integer)
DEBUG - ==> Parameters: ZambiaTest(String), ZM(String), 183(Integer)
==========================================
下面事务自动提交
==========================================
DEBUG - Initiating transaction commit
DEBUG - Committing JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]
DEBUG - Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2] after transaction
DEBUG - Returning JDBC Connection to DataSource

注意事项

1. 事务

由于在 Spring 集成的情况下,事务连接由 Spring 管理(SpringManagedTransaction),所以这里不需要手动关闭 sqlSession,在这里手动提交(commit)或者回滚(rollback)也是无效的。

2. 批量提交

批量提交只能应用于 insert, update, delete。

并且在批量提交使用时,如果在操作同一 SQL 时中间插入了其他数据库操作,就会让批量提交方式变成普通的执行方式,所以在使用批量提交时,要控制好 SQL 执行顺序。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/isea533/article/details/80922305

jb51ewm.png

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

转载于:https://www.cnblogs.com/wangdaijun/p/11045057.html

你可能感兴趣的文章
Java参考资料
查看>>
goto语句
查看>>
简单的车票管理系统
查看>>
2016年10月25 草堂随笔1 ModelState.IsValid
查看>>
Jenkins Pipelines Summary
查看>>
倾斜摄影 实景三维建模软件photoscan教程
查看>>
Actiion Func ;Donet framework 中已经定义好的委托
查看>>
Python 模块之 xlrd (Excel文件读写)
查看>>
再探@font-face及webIcon制作
查看>>
BZOJ.4212.神牛的养成计划(Trie 可持久化Trie)
查看>>
【unityd--初始学习四--3d世界中的物体移动及角度变换】
查看>>
电脑cmos是什么?和bois的区别?
查看>>
REST WCF 使用Stream进行Server与Client交互
查看>>
Python数据分析之Matplotlib绘制柱状图
查看>>
Django组件之contenttype
查看>>
刘江的博客
查看>>
SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
查看>>
Unity 2D游戏开发教程之为游戏场景添加多个地面
查看>>
Java日期时间处理
查看>>
hyperledger fabric 1.0.5 分布式部署 (七)
查看>>