MyBatis源码-解读Executor的三个实现类之BatchExecutor(批处理执行器)

在这里插入图片描述

Pre

MyBatis源码-深入理解MyBatis Executor的设计思想

工程部分见
MyBatis源码- SqlSession门面模式 & selectList 源码解析

在这里插入图片描述

实际中,我们都是面向SqlSession编程的,不会直接调用Executor来执行业务逻辑,这里我们仅仅是为了深入了解下Executor体系架构才这么搞的,切记。


Executor 执行器

在这里插入图片描述


接口继承关系

在这里插入图片描述

这里我们重点看下Executor的 三个实现子类。

分别是:SimpleExecutor(简单执行器)、ReuseExecutor(重用执行器)、BatchExecutor(批处理执行器)。


BatchExecutor(重用执行器)

BatchExecutor 仅对修改操作(包括删除)有效哈 ,对 select操作是不起作用。

BatchExecutor 主要是用于做批量更新操作的 ,底层会调用Statement的 executeBatch()方法实现批量操作

入门小demo

 @Test
    public void testBatchExecutor() throws SQLException {
        // 通过factory.openSession().getConnection()实例化JdbcTransaction ,用于构建BatchExecutor
        jdbcTransaction = new JdbcTransaction(factory.openSession().getConnection());

        // 实例化BatchExecutor
        BatchExecutor executor = new BatchExecutor(configuration, jdbcTransaction);

        // 映射SQL
        ms = configuration.getMappedStatement("com.artisan.UserMapper.updateById");

        Map map = new HashMap();
        map.put("arg0",1);
        map.put("arg1","222");

        // 调用doUpdate
        executor.doUpdate(ms,map);
        executor.doUpdate(ms,map);

        // 刷新
        executor.doFlushStatements(false);
        // 提交  否则不生效
        executor.commit(true);
     
    }

在这里插入图片描述


源码

在这里插入图片描述

currentSql 全局变量, 非线程安全

statementList 缓存 statement
batchResultList 缓存 返回结果

 @Override
  public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
    final Configuration configuration = ms.getConfiguration();
    final StatementHandler handler = configuration.newStatementHandler(this, ms, parameterObject, RowBounds.DEFAULT, null, null);
    final BoundSql boundSql = handler.getBoundSql();
    final String sql = boundSql.getSql();
    final Statement stmt; 
    
   //  当前sql , 并且是当前statement
    if (sql.equals(currentSql) && ms.equals(currentStatement)) {
      int last = statementList.size() - 1;
      stmt = statementList.get(last);
      applyTransactionTimeout(stmt);
      handler.parameterize(stmt);//fix Issues 322
      BatchResult batchResult = batchResultList.get(last);
      batchResult.addParameterObject(parameterObject);
    } else {
      Connection connection = getConnection(ms.getStatementLog());
      stmt = handler.prepare(connection, transaction.getTimeout());
      handler.parameterize(stmt);    //fix Issues 322
      currentSql = sql;
      currentStatement = ms;
      statementList.add(stmt);
      batchResultList.add(new BatchResult(ms, sql, parameterObject));
    }
    handler.batch(stmt);
    return BATCH_UPDATE_RETURN_VALUE;
  }

在这里插入图片描述

在这里插入图片描述


BatchExecutor VS ReuseExecutor

看输出 和 ReuseExecutor 感觉差不多,其实是有区别的

  • ReuseExecutor : 设置参数,执行,获取返回结果,然后在设置参数,执行,获取返回结果

  • BatchExecutor: 批量设置参数 , 执行 ,获取返回结果。

BatchExecutor仅执行一次,ReuseExecutor 执行多次

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值