mybatis源码阅读

Posted jas0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis源码阅读相关的知识,希望对你有一定的参考价值。

通过SqlSessionFactory 创建 SqlSession

// 通过SqlSessionFactory 获取创建一个SqlsessionSqlSession 
SqlSession 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();
      // 获取事务工厂 即 jdbc的
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      // 通过事务工厂创建一个事务
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      // 事务 和 excutorType 创建一个默认的Excutor
      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();
    }
  }
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;
  }

以上是关于mybatis源码阅读的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你阅读mybatis核心源码,掌握底层工作原理与设计思想

手把手教你阅读mybatis核心源码,掌握底层工作原理与设计思想

MyBatis源码阅读

#yyds干货盘点# mybatis源码解读:executor包(语句处理功能)

MyBatis 源码分析系列文章合集

mybatis源码阅读-helloword