下一篇:通过代理对象执行CUD的源码分析
获取SqlSession对象
加断点,debug进入openSession()方法,
可以看到openSession()方法返回的是openSessionFromDataSource()方法执行结果,该方法的第一个参数是configuration.getDefaultExecutorType()
如下图:configuration.getDefaultExecutortype()获取默认的执行方法,默认的执行类型为
“SIMPLE" .
进入openSessionFromDataSource()方法
该方法的第一个参数execType,值为”SIMPLE“
先看看ExecutorType都有哪些执行类型
可以看到有3种执行类型
再回到openSessionFromDataSource()方法
可以看到,通过configuration.newExecutor()方法返回一个Executor对象
方法参数execType作为configuration.newExecutor(tx,execType)方法的参数
进入newExecutor(),看到该方法不直接返回SimpleExecutor(),而是进行拦截
根据不同的类型execType,产生不同的Executor,现在获取了一个执行器->SimpleExecutor,但是没有直接返回,而是会对执行器进行拦截操作
executor = (Executor) interceptorChain.pluginAll(executor)
通过装饰模式,将刚才产生的executor包装成一个更加强大的executor。
进入这个interceptorChain.pluginAll()方法,看看如何拦截
作用:以后如果我们要给MyBatis写自己的插件,就可以通过拦截器实现。
插件开发: 1 写插件 2放入拦截器
再回到openSession()方法返回的openSessionFromDataSource()方法
刚刚拦截过的Executor对象,作为DefaultSqlSession()方法参数,
返回DefaultSqlSession(configuration,executor,autoCommit)configuration里面有增删改查标签,executor是执行器,可以执行configuration中的配置信息
现在就是:openSession() ⟵openSessionFromDataSource() ⟵ DefaultSqlSession(configuration,executor,autoCommit)对象