注
一下内容都是根据尚硅谷2018年mybatis源码解析做的课程笔记,视频链接:
https://www.bilibili.com/video/BV1mW411M737?p=74&share_source=copy_web
另外特别感谢雷丰阳老师的讲解
初始化完成后,接着看怎么通过openSession获取SqlSession
也就是这段代码
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession=null;
if (factory != null){
sqlSession=factory.openSession();
}
return sqlSession;
点进去openSession方法,是一个接口,快捷键ctrl+alt,点击接口进入实现类
DefaultSqlSessionFactory 这个类
其中this.configuration.getDefaultExecutorType()是一个执行器,在文档中有详细说明,默认是simple
public SqlSession openSession() {
return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);
}
进入openSessionFromDataSource
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
}
return var8;
}
在这个方法中,它获取了信息并做了事物处理,然后this.configuration.newExecutor对象
看看怎么new这个对象
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? this.defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Object 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 (this.cacheEnabled) {
executor = new CachingExecutor((Executor)executor);
}
Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
return executor;
}
它根据不同类型,创建出不同的Executor,如果有二级缓存,它就会进入这个判断
if (this.cacheEnabled) {
executor = new CachingExecutor((Executor)executor);
}
然后包装Executor
public CachingExecutor(Executor delegate) {
this.delegate = delegate;
delegate.setExecutorWrapper(this);
}
执行增删改查用Executor执行
最后它还调用了this.interceptorChain.pluginAll使用每一个拦截器重新包装再返回
Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
public Object pluginAll(Object target) {
Interceptor interceptor;
for(Iterator var2 = this.interceptors.iterator(); var2.hasNext(); target = interceptor.plugin(target)) {
interceptor = (Interceptor)var2.next();
}
return target;
}
Executor处理后返回,又赋值给了DefaultSqlSession
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
创建了DefaultSqlSession包含了全局对象this.configuration,以及处理后的Executor
然后再返回DefaultSqlSession
雷老师的流程图