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

 我们接着上一篇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)的封装返回处理的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,664评论 0 4
  • # 前言 在java程序员的世界里,最熟悉的开源软件除了 Spring,Tomcat,还有谁呢?当然是 Mybat...
    莫那一鲁道阅读 3,333评论 3 11
  • 今天星期天,注定了一个人会很无聊~最近的皮肤糟糕透了,便想到去做个护肤。 自已的皮肤一直是敏感肌和...
    AllfeHuang阅读 122评论 0 0
  • 儿时住在乡下,经常没电,夏天的时候,大人小孩忙碌了一天,吃过晚饭,大家简单地洗刷了之后,都喜欢到巷子口的晒...
    喜风SanPedroSula阅读 387评论 3 0
  • 最喜欢这种暖暖的色调
    King_哒哒阅读 175评论 0 2