Mybatis获取SqlSession(3)2018-08-17

2018-08-19  本文已影响0人  Seetheworl_6675

 我们接着上一篇Mybatis(2),我们来看看上面获取SqlSession:
我们从mybatis主要构件的执行流程:

mybatis.png

我们先来SqlSessionFactory是什么获取SqlSession的:SqlSessionFactory.openSession():

public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }

 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      //从上下文中获取当前环境
      final Environment environment = configuration.getEnvironment();
      //从当前环境中获取事物工厂
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      //创建事物对象
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //Executor 执行器
      final Executor executor = configuration.newExecutor(tx, execType);
      //创建SqlSession
      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();
    }
  }

这里我们关注下configuration.newExecutor(tx, execType):

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的类型:
ExecutorType.SIMPLE: 这个执行器类型不做特殊的事情。它为每个语句的执行创建一个新的预处理语句。
ExecutorType.REUSE: 这个执行器类型会复用预处理语句。
ExecutorType.BATCH: 这个执行器会批量执行所有更新语句。

接着我们关注下:

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

public class InterceptorChain {

  private final List<Interceptor> interceptors = new ArrayList<Interceptor>();

  public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }

  public void addInterceptor(Interceptor interceptor) {
    interceptors.add(interceptor);
  }
  
  public List<Interceptor> getInterceptors() {
    return Collections.unmodifiableList(interceptors);
  }

}

以上的代码其实调用plugs链对目标类(Executor)继续进一步的处理。这里plugs我们后续专门类了解,这里只需要知道下在获取SqlSession是初时候Execute执行器,通过plugs可以对Execute进行处理。

这边顺便提下SqlSession的四大对象:
1、Executor 执行器:来调度StatementHandler、ParameterHandler、ResultHandler等来执行对应的sql。
2、StatementHandler:数据会话器使用数据库的Statement(PreparedStatement)执行操作,它是四大对象的核心,起到承上启下的作用。
3、ParameterHandler:用于Sql对参数的处理
4、ResultHandler:进行最后的数据集(ResultSet)的封装返回处理的。

上一篇下一篇

猜你喜欢

热点阅读