很多人在用 mybatis 或者 通用 mapper 時,經(jīng)常會問有沒有批量插入和批量更新的方法。
實際上許多時候沒必要用<foreach>
去實現(xiàn)特別復(fù)雜的批量操作。直接通過 mybatis 的 batch 方式執(zhí)行增刪改方法即可。
下面是一個批量用法的例子:
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); |
后續(xù)通過sqlsession
直接執(zhí)行方法,或者獲取的mapper
接口,都使用的批量提交方式。
上述代碼執(zhí)行過程中輸出的日志如下:
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)
==========================================
...省略中間部分參數(shù)
==========================================
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)
==========================================
...省略中間部分參數(shù)
==========================================
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)
==========================================
...省略中間部分參數(shù)
==========================================
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)
==========================================
...省略中間部分參數(shù)
==========================================
debug - ==> parameters: zairetest(string), zr(string), 182(integer)
debug - ==> parameters: zambiatest(string), zm(string), 183(integer)
==========================================
下面事務(wù)自動提交
==========================================
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. 事務(wù)
由于在 spring 集成的情況下,事務(wù)連接由 spring 管理(springmanagedtransaction),所以這里不需要手動關(guān)閉 sqlsession,在這里手動提交(commit)或者回滾(rollback)也是無效的。
2. 批量提交
批量提交只能應(yīng)用于 insert, update, delete。
并且在批量提交使用時,如果在操作同一sql時中間插入了其他數(shù)據(jù)庫操作,就會讓批量提交方式變成普通的執(zhí)行方式,所以在使用批量提交時,要控制好 sql 執(zhí)行順序。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/isea533/article/details/80922305