mybatis随笔二之SqlSessionFactory

简介: 在上一篇文章我们已经得到了DefaultSqlSessionFactory@Override public SqlSession openSession() { return openSessionFromDataSource(configuration.
在上一篇文章我们已经得到了DefaultSqlSessionFactory

@Override
  public SqlSession openSession() {
    return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
  }
在configuration内部默认指定了protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
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);
      final Executor executor = configuration.newExecutor(tx, execType);
      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();
    }
  }
从代码中我们可知开启了一个jdbc事务,并调用newExecutor得到一个执行期
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;
  }
如果开启了cache,那么会得到一个CachingExecutor,这采用的是装饰设计模式,它的语句执行实际是交给内部的delegate来执行的。
里面值得注意的是使用了(Executor) interceptorChain.pluginAll(executor)将所有的拦截器注入进去形成一个拦截链。
public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }
@Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
其实Plugin是mybatis的一个工具类,用来方便生成代理对象的,注意这个地方只会生成拦截目标是Executor的拦截类的代理对象,如下所示这种就不会调用。
@Intercepts({
        @Signature(type = StatementHandler.class,
                method = "prepare",
                args = {Connection.class})
})
然后返回DefaultSqlSession的一个实例对象
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
    this.configuration = configuration;
    this.executor = executor;
    this.dirty = false;
    this.autoCommit = autoCommit;
  }

至此DefaultSqlSessionFactory创建DefaultSqlSession的过程完成。


 

相关文章
|
4月前
|
SQL 缓存 Java
MyBatis原理分析之获取SqlSessionFactory
MyBatis原理分析之获取SqlSessionFactory
96 0
|
3月前
|
缓存 Java 数据库连接
一文彻底搞懂Mybatis系列(十)之SqlSession、SqlSessionFactory和SqlSessionFactoryBuilder详解
一文彻底搞懂Mybatis系列(十)之SqlSession、SqlSessionFactory和SqlSessionFactoryBuilder详解
314 1
|
7月前
|
XML JavaScript Java
MyBatis SqlSessionFactory的两种创建方式
MyBatis SqlSessionFactory的两种创建方式
71 0
|
SQL 安全 Java
MyBatis——通过占位符实现insert操作、一些重要对象(SqlSessionFactory、SqlSession)的理解
MyBatis——通过占位符实现insert操作、一些重要对象(SqlSessionFactory、SqlSession)的理解
MyBatis——通过占位符实现insert操作、一些重要对象(SqlSessionFactory、SqlSession)的理解
|
XML Java 数据库连接
MyBatis源码分析之——配置解析创建SqlSessionFactory的过程
大家应该都知道Mybatis源码也是对Jbdc的再一次封装,不管怎么进行包装,还是会有获取链接、preparedStatement、封装参数、执行这些步骤的。
134 0
|
XML Java 数据库连接
深入理解mybatis原理, Mybatis初始化SqlSessionFactory机制详解
    对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外。本章将通过以下几点详细介绍MyBatis的初始化过程。
1342 0
|
28天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
39 1
|
3月前
|
Java 数据库连接 Maven
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
SSM框架整合:掌握Spring+Spring MVC+MyBatis的完美结合!
|
22天前
|
敏捷开发 监控 前端开发
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
Spring+SpringMVC+Mybatis的分布式敏捷开发系统架构
53 0