扒开 SqlSession 的外衣

老规矩,先上案例代码,按照这个案例一步一步的搞定Mybatis源码。mysql

public class MybatisApplication {
    public static final String URL = "jdbc:mysql://localhost:3306/mblog";
    public static final String USER = "root";
    public static final String PASSWORD = "123456";

    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession = sqlSessionFactory.openSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            System.out.println(userMapper.selectById(1));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqlSession.close();
        }
    }

前面咱们已经讲了Mybatis是如何解析相关配置文件的,若是怕迷路,仍是建议先看前一篇文章:算法

Mybatis是如何解析配置文件的?看完终于明白了
sql

因为不少小伙伴在催,说Mybatis源码系列好像什么时候才有下文了,为此老田熬夜写了这篇。设计模式

图片

继续开撸~~
缓存

SqlSession sqlSession = sqlSessionFactory.openSession();

前面那篇文章已经分析了,这里的sqlSessionFactory其实就是DefaultSqlSessionFactory。session

因此这里,咱们就从DefaultSqlSessionFactory里的openSession方法开始。mybatis

public class DefaultSqlSessionFactory implements SqlSessionFactory {

  private final Configuration configuration;

  public DefaultSqlSessionFactory(Configuration configuration) {
    this.configuration = configuration;
  }
  //建立session,这个方法直接调用本类中的另一个方法
  @Override
  public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), nullfalse);
  }
  //实际上是调用这个方法
  private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      //对应xml标签<environments> ,这个在配置文件解析的时候就已经存放到configuration中了。
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //建立一个executor来执行SQL  
      final Executor executor = configuration.newExecutor(tx, execType);
      //这里也说明了,为何咱们代码里的SqlSession是DefaultSqlSession
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }
  
    private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
    if (environment == null || environment.getTransactionFactory() == null) {
      return new ManagedTransactionFactory();
    }
    return environment.getTransactionFactory();
  }

这个方法中的主要内容有:

图片

下面咱们就来逐个攻破。app

建立事务Transaction

事务工厂类型能够配置为JDBC类型或者MANAGED类型。ide

图片

JdbcTransactionFactory生产JdbcTransaction。源码分析

ManagedTransactionFactory生产ManagedTransaction。

若是配置的JDBC,则会使用Connection对象的commit()、rollback()、close()方法来管理事务。

若是咱们配置的是MANAGED,会把事务交给容器来管理,好比JBOSS,Weblogic。由于咱们是本地跑的程序,若是配置成MANAGED就会不有任何事务。

可是,若是咱们项目中是Spring集成Mybatis,则没有必要配置事务,由于咱们会直接在applicationContext.xml里配置数据源和事务管理器,从而覆盖Mybatis的配置。

建立执行器Executor

调用configuration的newExecutor方法建立Executor。

final Executor executor = configuration.newExecutor(tx, execType);
//Configuration中
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    //第一步
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    //第二步
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    //第三步
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

此方法分三个步骤。

第一步:建立执行器

Executor的基本类型有三种:

public enum ExecutorType {
  SIMPLE, REUSE, BATCH
}

SIMPLE为默认类型。

为何要让抽象类BaseExecutor实现Executor接口,而后让具体实现类继承抽象类呢?

这就是模板方法模式的实现。

模板方法模式就是定义一个算法骨架,并容许子类为一个或者多个步骤提供实现。模板方法是得子类能够再不改变算法结构的状况下,从新定义算法的某些步骤。

关于模板方法模式推荐阅读:

如何快速掌握模板方法模式

抽象方法是在子类汇总实现的,每种执行器本身实现本身的逻辑,BaseExecutor最终会调用到具体的子类中。

抽象方法
protected abstract int doUpdate(MappedStatement ms, Object parameter) throws SQLException;

protected abstract List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException;

protected abstract <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException;

protected abstract <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql)  throws SQLException;

第二步:缓存装饰

在上面代码中的第二步

if (cacheEnabled) {
      executor = new CachingExecutor(executor);
}

若是cacheEnabled=true,会用装饰器设计模式对Executor进行装饰。

第三步:插件代理

缓存装饰完后,就会执行

executor = (Executor) interceptorChain.pluginAll(executor);

这里会对Executor植入插件逻辑。

好比:分页插件中就须要把插件植入的Executor

图片

好了,到此,执行器建立的就搞定了。

建立DefaultSqlSession对象

把前面解析配置文件建立的Configuration对象和建立的执行器Executor赋给DefaultSqlSession中的属性。

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
  this.configuration = configuration;
  this.executor = executor;
  this.dirty = false;
  this.autoCommit = autoCommit;
}

到这里,SqlSession(DefaultSqlSession)对象就建立完毕。

总结

本文咱们讲了如何建立SqlSession的几个步骤,最后咱们得到一个DefaultSqlSession对象,里面包含了执行器Executor和配置对象Configuration。Executor是SQL的实际执行对象。Configuration里保存着配置文件内容。

本文源码分析的整个流程以下图:

图片


码字不易,点个

相关文章
相关标签/搜索