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

编写mybatis批量处理插件

编写该插件的目的是项目中经常会有一些需要批处理的情况,当然Mysql支持insert() values(),()....,()语法,可以间接达到批量提交的目的。但是在update的时候就不行了。
本插件基于mybatis-3.4.4。实现原理:如果上下文中需要开启批处理,那么我就用BatchExecutor代替原先的SimpleExecutor执行器。 插件实现类:

@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;

    @Override
    public 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){
                //执行executeBatch
                batchExecutor.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());
    }

    @Override
    public Object plugin(Object target) {
        //包装插件
        return Plugin.wrap(target,this);
    }

    @Override
    public void setProperties(Properties properties) {
        this.batchCommit = Integer.parseInt(properties.getProperty("batchCommit","0"));
    }
}

spring-boot2 自动配置类

@Configuration
@ConditionalOnBean({SqlSessionFactory.class})
@EnableConfigurationProperties({MybatisBatchProperties.class})
@AutoConfigureAfter({MybatisAutoConfiguration.class})
public class MybatisBatchAutoConfiguration {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @Autowired
    private MybatisBatchProperties mybatisBatchProperties;

    @PostConstruct
    public 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项目中引用JAR包

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

在需要批量提交的代码开启批处理:

MybatisBatchHelper.startBatch();

测试情况

插入

代码

@Override
    public 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

更新

代码

@Override
    public 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

转载于:https://my.oschina.net/u/3217171/blog/3044928

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给您提供一个MyBatis框架中的分页PageHelper的具体使用案例。 1.引入依赖 在Maven中引入PageHelper的依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.2.0</version> </dependency> ``` 2.配置件 在MyBatis的配置文件中配置PageHelper件: ```xml <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="dialect" value="mysql"/> </plugin> </plugins> ``` 这里使用的是MySQL数据库,如果是其他数据库,可以将dialect属性值设置为相应的数据库类型。 3.使用件 在需要进行分页查询的方法中,设置分页参数: ```java public List<User> getUsers(int pageNum, int pageSize) { //设置分页参数 PageHelper.startPage(pageNum, pageSize); //执行查询 List<User> userList = userMapper.selectUsers(); //返回查询结果 return userList; } ``` 这里的参数pageNum表示当前页码,pageSize表示每页显示的记录数。 4.获取分页信息 在查询结果中,可以通过PageInfo对象获取分页信息: ```java public PageInfo<User> getUsers(int pageNum, int pageSize) { //设置分页参数 PageHelper.startPage(pageNum, pageSize); //执行查询 List<User> userList = userMapper.selectUsers(); //获取分页信息 PageInfo<User> pageInfo = new PageInfo<>(userList); //返回分页信息 return pageInfo; } ``` 通过PageInfo对象,可以获取总记录数、总页数、当前页码等分页信息。 以上就是MyBatis框架中的分页PageHelper的具体使用案例,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值