《Mybatis 手撸专栏》第7章:SQL执行器的定义和实现

Mybatis 手撸专栏

第7章:SQL执行器的定义和实现

本章我们将深入探讨 Mybatis 中的 SQL 执行器的定义和实现。在 Mybatis 中,SQL 执行器负责将 SQL 语句发送到数据库执行,并返回结果。

1. SQL 执行器的定义

SQL 执行器是 Mybatis 中的核心组件之一,它负责执行一条或多条 SQL 语句,将结果返回给调用方。SQL 执行器的定义由接口 Executor 表示,该接口定义了以下方法:

public interface Executor {
  
  // 查询单个结果
  <T> T query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler<T> resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;
  
  // 查询多个结果
  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler<T> resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

  // 更新操作
  int update(MappedStatement ms, Object parameter) throws SQLException;
  
  // 批量执行
  int batch(MappedStatement ms, Object parameter) throws SQLException;
  
  // 提交事务
  void commit(boolean required) throws SQLException;
  
  // 回滚事务
  void rollback(boolean required) throws SQLException;
  
  // 清空本地缓存
  void clearLocalCache();
  
  // 延迟加载
  void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);
  
  // 获取事务
  Transaction getTransaction();
  
  // 关闭 Executor
  void close(boolean forceRollback);
  
  // 判断 Executor 是否已关闭
  boolean isClosed();
  
  // 设置包装的 Executor
  void setExecutorWrapper(Executor executor);
}

SQL 执行器的核心方法包括查询单个结果 query、查询多个结果 query、更新操作 update、批量执行 batch等。此外,SQL 执行器还提供了事务管理、本地缓存管理等功能。

2. SQL 执行器的实现

在 Mybatis 中,默认提供了三种 SQL 执行器的实现,分别是 SimpleExecutorReuseExecutorBatchExecutor。这三个执行器都是通过装饰器模式对 BaseExecutor 进行了扩展实现的。

2.1 SimpleExecutor

SimpleExecutor 是 Mybatis 中最简单的 SQL 执行器实现。它对每一次 SQL 执行都会创建一个新的 Statement 对象,并直接执行 SQL。

SimpleExecutor 的源码结构如下:

public class SimpleExecutor extends BaseExecutor {
  
  // ...
  
  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler<T> resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
      Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, rowBounds, resultHandler, boundSql);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }
  
  // ...
  
}

doQuery 方法中,SimpleExecutor 创建了一个新的 StatementHandler 对象,并通过该对象来执行查询操作。

2.2 ReuseExecutor

ReuseExecutor 是 Mybatis 中复用 Statement 的 SQL 执行器实现。它会缓存 Statement 对象,并且在下一次执行相同 SQL 语句时进行复用。

ReuseExecutor 的源码结构如下:

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

doQuery 方法中,ReuseExecutor 创建了一个新的 StatementHandler 对象,并通过该对象来执行查询操作。与 SimpleExecutor 不同的是,ReuseExecutor 并不关闭 Statement 对象,而是缓存起来以备下次复用。

2.3 BatchExecutor

BatchExecutor 是 Mybatis 中用于批量执行 SQL 语句的 SQL 执行器实现。它会将多条 SQL 语句打包成一次数据库调用,一次性执行。

BatchExecutor 的源码结构如下:

public class BatchExecutor extends BaseExecutor {
  
  // ...
  
  @Override
  public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
    Configuration configuration = ms.getConfiguration();
    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, RowBounds.DEFAULT, null, null);
    Statement stmt = prepareStatement(handler, ms.getStatementLog());
    return handler.update(stmt);
  }
  
  // ...
  
}

doUpdate 方法中,BatchExecutor 创建了一个新的 StatementHandler 对象,并通过该对象来执行更新操作。

3. SQL 执行器的选择和配置

在实际的应用中,我们可以根据具体的需求和性能要求来选择合适的 SQL 执行器。

  • 如果每次执行 SQL 都需要重新创建 Statement 对象,那么可以选择 SimpleExecutor。
  • 如果希望能够复用 Statement 对象,并且查询频率较高,可以选择 ReuseExecutor。
  • 如果需要批量执行多条 SQL 语句,可以选择 BatchExecutor。

同时,我们可以通过 Mybatis 的配置文件对 SQL 执行器进行配置。以下是一个简单的配置示例:

<!-- MybatisConfig.xml -->
<configuration>
  <settings>
    <!-- 配置默认的 SQL 执行器 -->
    <setting name="defaultExecutorType" value="SIMPLE"/>
  </settings>
</configuration>

在上述示例中,我们可以通过 <settings> 标签来配置默认的 SQL 执行器。在这里,我们将默认的 SQL 执行器设置为 SimpleExecutor。

4. 总结

通过本章的学习,我们了解了 Mybatis 中 SQL 执行器的定义和实现。我们知道 SQL 执行器负责将 SQL 语句发送到数据库执行,并返回结果。

在实际的应用中,我们可以根据具体的性能需求选择合适的 SQL 执行器,并通过 Mybatis 的配置文件对 SQL 执行器进行配置。

希望本章的内容对您理解和应用 Mybatis 中的 SQL 执行器有所帮助。下一章,我们将继续深入探讨 Mybatis 的更多重要功能,敬请期待!

全部评论

相关推荐

投递腾讯等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务