国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

MyBatis通過BATCH批量提交的方法

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

很多人在用 MyBatis 或者 通用 Mapper 時(shí),經(jīng)常會(huì)問有沒有批量插入和批量更新的方法。

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

下面是一個(gè)批量用法的例子:

@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,通過下面方法獲取了一個(gè)可以批量提交的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ù)自動(dòng)提交
==========================================
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

注意事項(xiàng)

1. 事務(wù)

由于在 Spring 集成的情況下,事務(wù)連接由 Spring 管理(SpringManagedTransaction),所以這里不需要手動(dòng)關(guān)閉 sqlSession,在這里手動(dòng)提交(commit)或者回滾(rollback)也是無效的。

2. 批量提交

批量提交只能應(yīng)用于 insert, update, delete。

并且在批量提交使用時(shí),如果在操作同一SQL時(shí)中間插入了其他數(shù)據(jù)庫操作,就會(huì)讓批量提交方式變成普通的執(zhí)行方式,所以在使用批量提交時(shí),要控制好 SQL 執(zhí)行順序。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 彰化市| 灵川县| 壶关县| 通江县| 祁东县| 古蔺县| 定州市| 莱芜市| 承德市| 密山市| 灵寿县| 通化县| 依安县| 搜索| 镇雄县| 潼南县| 磴口县| 余庆县| 罗源县| 仲巴县| 阳谷县| 鹿泉市| 唐山市| 体育| 阳泉市| 政和县| 宣汉县| 普格县| 墨竹工卡县| 龙南县| 上栗县| 报价| 乌海市| 孟村| 手机| 张家川| 奉贤区| 金昌市| 宁晋县| 玛沁县| 海南省|