很多人在用 mybatis 或者 通用 mapper 时,经常会问有没有批量插入和批量更新的方法。
实际上许多时候没必要用<foreach>
去实现特别复杂的批量操作。直接通过 mybatis 的 batch 方式执行增删改方法即可。
下面是一个批量用法的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@autowired private sqlsessionfactory sqlsessionfactory; @transactional (rollbackfor = exception. class ) @override public void batchtest() { sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch); countrymapper mapper = sqlsession.getmapper(countrymapper. class ); list<country> 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
:
1
|
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