日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一个mybatis处理batch的插件,类似于pageHelper插件

發(fā)布時(shí)間:2025/3/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个mybatis处理batch的插件,类似于pageHelper插件 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

編寫mybatis批量處理插件

編寫該插件的目的是項(xiàng)目中經(jīng)常會(huì)有一些需要批處理的情況,當(dāng)然Mysql支持insert() values(),()....,()語法,可以間接達(dá)到批量提交的目的。但是在update的時(shí)候就不行了。
本插件基于mybatis-3.4.4。實(shí)現(xiàn)原理:如果上下文中需要開啟批處理,那么我就用BatchExecutor代替原先的SimpleExecutor執(zhí)行器。 插件實(shí)現(xiàn)類:

@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),@Signature(type = Executor.class, method = "commit", args = {boolean.class}),@Signature(type = Executor.class, method = "close", args = {boolean.class})} ) public class BatchHelperIntercept implements Interceptor {private int batchCommit = 0;@Overridepublic Object intercept(Invocation invocation) throws Throwable {Executor target = (Executor)invocation.getTarget();Method method = invocation.getMethod();if(StringUtils.equals(method.getName(),"update") && MybatisBatchHelper.needBatch()) {MappedStatement ms = (MappedStatement)invocation.getArgs()[0];//需要批量提交BatchExecutor batchExecutor = MybatisBatchHelper.getBatchExecutor();if (batchExecutor == null) {batchExecutor = new BatchExecutor(ms.getConfiguration(), target.getTransaction());MybatisBatchHelper.setBatchExecutor(batchExecutor);}Object resObject = method.invoke(batchExecutor, invocation.getArgs());MybatisBatchHelper.increment();if(this.batchCommit > 0 && MybatisBatchHelper.getBatchCommit() == this.batchCommit){//執(zhí)行executeBatchbatchExecutor.flushStatements();}return resObject;}BatchExecutor batchExecutor = MybatisBatchHelper.getBatchExecutor();boolean hasBatchExecutor = batchExecutor != null;if(StringUtils.equals(method.getName(),"commit") && hasBatchExecutor){return method.invoke(batchExecutor, invocation.getArgs());}if(StringUtils.equals(method.getName(),"close") && hasBatchExecutor){MybatisBatchHelper.clear();return method.invoke(batchExecutor, invocation.getArgs());}return method.invoke(target,invocation.getArgs());}@Overridepublic Object plugin(Object target) {//包裝插件return Plugin.wrap(target,this);}@Overridepublic void setProperties(Properties properties) {this.batchCommit = Integer.parseInt(properties.getProperty("batchCommit","0"));} }

spring-boot2 自動(dòng)配置類

@Configuration @ConditionalOnBean({SqlSessionFactory.class}) @EnableConfigurationProperties({MybatisBatchProperties.class}) @AutoConfigureAfter({MybatisAutoConfiguration.class}) public class MybatisBatchAutoConfiguration {@Autowiredprivate List<SqlSessionFactory> sqlSessionFactoryList;@Autowiredprivate MybatisBatchProperties mybatisBatchProperties;@PostConstructpublic void addPageInterceptor() {BatchHelperIntercept interceptor = new BatchHelperIntercept();Properties properties = mybatisBatchProperties.getProperties();interceptor.setProperties(properties);Iterator<SqlSessionFactory> it = this.sqlSessionFactoryList.iterator();while(it.hasNext()) {SqlSessionFactory sqlSessionFactory = it.next();sqlSessionFactory.getConfiguration().addInterceptor(interceptor);}} }

用法

在spring-boot2項(xiàng)目中引用JAR包

<dependency><groupId>com.github.liuax</groupId><artifactId>mybatis-batch-starter</artifactId><version>1.0.0</version> </dependency>

在需要批量提交的代碼開啟批處理:

MybatisBatchHelper.startBatch();

測試情況

插入

代碼

@Overridepublic void test1(){//MybatisBatchHelper.startBatch();for(int i = 0;i<5000;i++){OssParseLog log = new OssParseLog();log.setBatchId(i+"");log.setCrtTime(new Date());log.setName("aaaa");log.setHhmmss("112233");log.setType("test1");log.setApp("0001");baseManagr.insertSelective(log);}}

未開啟批處理的情況:

2019-05-03 08:27:09.429 DEBUG 11236 --- [ main] c.v.f.b.b.s.m.O.insertSelective : <== Updates: 1 84524:ms ok

開啟批處理的情況:

2019-05-03 09:17:40.355 DEBUG 13036 --- [ main] c.v.f.b.b.s.m.O.insertSelective : ==> Parameters: 4999(String), 112233(String), test1(String), 0001(String), aaaa(String), 2019-05-03 09:17:40.355(Timestamp) 1834:ms ok

更新

代碼

@Overridepublic void test2() {MybatisBatchHelper.startBatch();for(int i = 0;i<5000;i++){OssParseLog log = new OssParseLog();log.setBatchId(i+"");log.setCrtTime(new Date());log.setName("bbbbb");log.setHhmmss("112233");log.setType("test2");log.setApp("0001");Example example = Example.builder(OssParseLog.class).andWhere(Sqls.custom().andEqualTo("batchId",i+"")).build();baseManagr.updateSelectiveByExample(log,example);}}

未開啟批處理的情況:

2019-05-03 09:25:04.431 DEBUG 9224 --- [ main] c.v.f.b.b.s.m.O.updateByExampleSelective : <== Updates: 1 87424:ms ok

開啟批處理的情況:

2019-05-03 09:27:40.063 DEBUG 10984 --- [ main] c.v.f.b.b.s.m.O.updateByExampleSelective : ==> Parameters: 4999(String), 112233(String), test2(String), 0001(String), ccccc(String), 2019-05-03 09:27:40.063(Timestamp), 4999(String) 3744:ms ok

轉(zhuǎn)載于:https://my.oschina.net/u/3217171/blog/3044928

總結(jié)

以上是生活随笔為你收集整理的一个mybatis处理batch的插件,类似于pageHelper插件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。