myBatis的批量提交方式


MyBatis 批量提交 - BATCH

https://blog.csdn.net/isea533/article/details/80922305

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

 

實際上許多時候沒必要用 <foreach> 去實現特別復雜的批量操作。直接通過 MyBatis BATCH 方式執行增刪改方法即可。

 

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

 

@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

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 執行順序。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM