mybatis源码解析2_SqlSession

一下内容都是根据尚硅谷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

雷老师的流程图
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值