上一节中我们获取到的DefaultSqlSessionFactory,看看它是如何生产出SqlSession的。
//今天的重点:得到默认的DefaultSqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
我们一步一步往下看
//DefaultSqlSessionFactory
@Override
public SqlSession openSession() {
return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}
继续往下走
//DefaultSqlSessionFactory
/**
* 方法的入参是不是有点眼熟
* @param execType 执行器的类型 默认使用的是ExecutorType.SIMPLE
* @param level 事物的隔离级别
* @param autoCommit 是否自动提交事物
*/
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = configuration.getEnvironment();
//这里会得到默认的ManagedTransactionFactory
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
//由ManagedTransactionFactory创建相应的ManagedTransaction
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
//execType为ExecutorType.SIMPLE
//没有开启缓存就默认是SimpleExecutor 开了就是CachingExecutor
final Executor executor = configuration.newExecutor(tx, execType);
//得到默认的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();
}
}
从这里我们可以看到,都是很熟悉的东西,数据源、事物、执行器、还有我们的DefaultSqlSession,我们来看看具体都是怎么样得到它们的
mybatis的TransactionFactory
//DefaultSqlSessionFactory
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
if (environment == null || environment.getTransactionFactory() == null) {
return new ManagedTransactionFactory();//默认没有配置就是这个
}
return environment.getTransactionFactory();
}
mybatis的Transaction
@Override
//ManagedTransactionFactory
/**
* @param ds 数据源
* @param level 事物的隔离级别
* @param autoCommit 是否自动提交
*/
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
// Silently ignores autocommit and isolation level, as managed transactions are entirely
// controlled by an external manager. It's silently ignored so that
// code remains portable between managed and unmanaged configurations.
return new ManagedTransaction(ds, level, closeConnection);
}
看下相关的UML
mybatis的Executor
//Configuration
/**
*
* @param transaction 默认使用的ManagedTransaction
* @param executorType 默认使用的是ExecutorType.SIMPLE
* @return
*/
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) {//是否开启了缓存
//开启了就传入由上面得到执行器new一个CachingExecutor
//CachingExecutor使用了委派模式 前面三种执行器才是真正的执行者
executor = new CachingExecutor(executor);
}
//加到mybatis的拦截器中
executor = (Executor) interceptorChain.pluginAll(executor);
return executor;
}
看下相关的UML
SqlSession
//DefaultSqlSession
public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.dirty = false;
this.autoCommit = autoCommit;
}
几个入参都是前面说到的
看看相关的UML
到这里如何获取到SqlSession就结束,这部分比较简单,下一节我们来说说通过SqlSession如何来执行一条语句,执行的流程又是怎么样,得到的结果又是如何解析的