Mybatis执行ReuseExecutor(五)

MyBatis ReuseExecutor详解
本文详细介绍了MyBatis中的ReuseExecutor实现原理。该执行器通过缓存Statement来提高SQL执行效率,避免了相同SQL多次执行时重复创建Statement的问题。文章深入分析了其核心方法如doUpdate、doQuery及doFlushStatements等,并解释了如何通过hashMap存储和检索Statement。

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);
  }

}


评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值