MyBatis 經過 BATCH 批量提交

本文由 簡悅 SimpRead 轉碼, 原文地址 https://www.jb51.net/article/153382.htmjava

不少人在用 MyBatis 或者 通用 Mapper 時,常常會問有沒有批量插入和批量更新的方法。redis

實際上許多時候不必用<foreach> 去實現特別複雜的批量操做。直接經過 MyBatis 的 BATCH 方式執行增刪改方法便可。spring

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

@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
 
 
 
 
  
  
  
   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接口,都使用的批量提交方式。微信

上述代碼執行過程當中輸出的日誌以下:mybatis

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 DataSourceapp

注意事項ide

1. 事務學習

因爲在 Spring 集成的狀況下,事務鏈接由 Spring 管理(SpringManagedTransaction),因此這裏不須要手動關閉 sqlSession,在這裏手動提交(commit)或者回滾(rollback)也是無效的。

2. 批量提交

批量提交只能應用於 insert, update, delete。

而且在批量提交使用時,若是在操做同一 SQL 時中間插入了其餘數據庫操做,就會讓批量提交方式變成普通的執行方式,因此在使用批量提交時,要控制好 SQL 執行順序。

總結

以上就是這篇文章的所有內容了,但願本文的內容對你們的學習或者工做具備必定的參考學習價值,謝謝你們對腳本之家的支持。若是你想了解更多相關內容請查看下面相關連接

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

微信公衆號搜索 「 腳本之家 」 ,選擇關注

程序猿的那些事、送書等活動等着你

相關文章
相關標籤/搜索