麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > Java > 正文

MyBatis通過BATCH批量提交的方法

2024-07-14 08:43:19
字體:
供稿:網(wǎng)友

很多人在用 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)的支持。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲一区二区三区视频免费 | 欧美a在线观看 | www视频免费在线观看 | 色诱亚洲精品久久久久久 | 亚洲欧美国产高清va在线播放 | 免费在线观看国产精品 | 97色在线观看免费视频 | 久精品国产 | 黄色毛片视频在线观看 | 99精品视频网站 | 欧美巨乳在线观看 | 亚洲欧美在线视频免费 | 九九热这里只有精品8 | 欧美videofree性欧美另类 | 国产va在线观看免费 | 亚洲一级毛片 | 欧美日韩大片在线观看 | 天天透天天狠天天爱综合97 | 国产成年人在线观看 | 毛片视频播放 | 97中文字幕在线观看 | 亚洲精品一区二区三区在线看 | 午夜小影院 | 亚洲国产精品二区 | 久久久在线免费观看 | 欧美日韩在线免费观看 | av之家在线观看 | 黄色成人短视频 | 狠狠色成色综合网 | 九九热精品视频在线播放 | 亚洲午夜天堂吃瓜在线 | 草草久久久| 久久精品视频7 | 污在线观看网站 | 久久综合伊人 | 妇子乱av一区二区三区 | 久久毛片免费 | 欧美精品免费一区二区三区 | 毛片免费一区二区三区 | 91懂色| 蜜桃网站免费 |