很多人在用 MyBatis 或者 通用 Mapper 時,經(jīng)常會問有沒有批量插入和批量更新的方法。
實際上許多時候沒必要用<foreach>
去實現(xiàn)特別復(fù)雜的批量操作。直接通過 MyBatis 的 BATCH 方式執(zhí)行增刪改方法即可。
下面是一個批量用法的例子:
@Autowiredprivate SqlSessionFactory sqlSessionFactory;@Transactional(rollbackFor = Exception.class)@Overridepublic 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
:
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í)價值,謝謝大家對VeVb武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選