Mybatis执行ReuseExecutor(五)

ReuseExecutor顾名思义就是重复使用执行,其定义了一个Map<String, Statement>,将执行的sql作为key,将执行的Statement作为value保存,这样执行相同的sql时就可以使用已经存在的Statement,就不需要新创建了,源码及分析如下:

/**
 * @author Clinton Begin
 */
public class ReuseExecutor extends BaseExecutor {

  private final Map<String, Statement> statementMap = new HashMap<String, Statement>();

  public ReuseExecutor(Configuration configuration, Transaction transaction) {
    super(configuration, transaction);
  }

  @Override
  public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
	//获得配置文件
    Configuration configuration = ms.getConfiguration();
	//获得statementHandler 包括插件内容
    StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
	//转换为PrepareStatement
    Statement stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.update(stmt);
  }

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
    Statement stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.<E>query(stmt, resultHandler);
  }

  @Override
  public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
    for (Statement stmt : statementMap.values()) {
	  //关闭Statement
      closeStatement(stmt);
    }
	//清空sql
    statementMap.clear();
    return Collections.emptyList();
  }

  private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    BoundSql boundSql = handler.getBoundSql();
	//获得sql语句
    String sql = boundSql.getSql();
	//查看是否存在Statement
    if (hasStatementFor(sql)) {
	  //如果存在就获取Statement
      stmt = getStatement(sql);
    } else {
      Connection connection = getConnection(statementLog);
	  //否则通过连接创建一个Statement
      stmt = handler.prepare(connection);
	  //将sql语句及对应的Statement 保存到map中
      putStatement(sql, stmt);
    }
    handler.parameterize(stmt);
    return stmt;
  }

  private boolean hasStatementFor(String sql) {
    try {
	  //查看map中是否含有sql语句对应的Statement
      return statementMap.keySet().contains(sql) && !statementMap.get(sql).getConnection().isClosed();
    } catch (SQLException e) {
      return false;
    }
  }

  private Statement getStatement(String s) {
	//获得Sql语句对应的Statement
    return statementMap.get(s);
  }

  private void putStatement(String sql, Statement stmt) {
	//将sql语句及对应的Statement保存到map中
    statementMap.put(sql, stmt);
  }

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MyBatis 执行器是用于执行查询的主要代码。在 MyBatis 中,有三种类型的执行器:简单执行器(SimpleExecutor)、可重用执行器(ReuseExecutor)和批量执行器(BatchExecutor)。可以通过配置文件来设置使用哪种执行器。其中,ExecutorMyBatis执行器,它负责执行 SQL 语句并返回结果。 在 MyBatis 中,还有一个特殊的执行器称为 CachingExecutor,它是在 Executor 对象的基础上增加了二级缓存相关的功能。然而,由于实际使用中二级缓存的利弊不太平衡,往往被像 Redis 等产品所取代。 所以,MyBatis 执行器是用于执行查询的关键组件,根据不同的需求和配置,可以选择不同的执行器来执行 SQL 语句并返回查询结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis中的执行器(Executor)](https://blog.csdn.net/liming0025/article/details/118632419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis执行器](https://blog.csdn.net/xing_jian1/article/details/123967457)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值