MyBatis的执行流程
1、解析配置文件,生成Configuration对象
解析配置文件后,转化为Configuration对象如下图所示:
构建过程
>org.apache.ibatis.builder.xml.XMLConfigBuilder
>org.apache.ibatis.builder.xml.XMLMapperBuilder
>org.apache.ibatis.builder.xml.XMLStatementBuilder
>org.apache.ibatis.builder.SqlSourceBuilder
>org.apache.ibatis.scripting.xmltags.XMLScriptBuilder
>org.apache.ibatis.builder.annotation.MapperAnnotationBuilder
2、构建sqlStatment
>org.apache.ibatis.session.SqlSessionFactoryBuilder#build()
//1.Config.xml 文件解析
>org.apache.ibatis.builder.xml.XMLConfigBuilder#parse()
>org.apache.ibatis.builder.xml.XMLConfigBuilder#parseConfiguration()
>org.apache.ibatis.builder.xml.XMLConfigBuilder#mapperElement()
// 2.Mapper.xml 文件解析
>org.apache.ibatis.builder.xml.XMLMapperBuilder#parse()
>org.apache.ibatis.builder.xml.XMLMapperBuilder#configurationElement()
>org.apache.ibatis.builder.xml.XMLMapperBuilder#buildStatementFromContext()
//3.Statemen sql块解析
>org.apache.ibatis.builder.xml.XMLStatementBuilder#parseStatementNode
>org.apache.ibatis.builder.MapperBuilderAssistant#addMappedStatement()
// 4.动态SQL脚本解析
>org.apache.ibatis.scripting.xmltags.XMLLanguageDriver#createSqlSource()
>org.apache.ibatis.scripting.xmltags.XMLScriptBuilder#parseScriptNode()
>org.apache.ibatis.scripting.xmltags.XMLScriptBuilder#parseDynamicTags()
3、创建sqlSession会话
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSession(boolean)
>org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSessionFromDataSource
>org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory#newTransaction()
>org.apache.ibatis.session.Configuration#newExecutor()
>org.apache.ibatis.executor.SimpleExecutor#SimpleExecutor
>org.apache.ibatis.executor.CachingExecutor#CachingExecutor
//执行器插件包装
>org.apache.ibatis.plugin.InterceptorChain#pluginAll(executor)
>org.apache.ibatis.session.defaults.DefaultSqlSession#DefaultSqlSession()
4、方法执行StatmentHandler
StatementHandler 源码调用链
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#queryFromDatabase
>org.apache.ibatis.session.Configuration#newStatementHandler
org.apache.ibatis.executor.statement.BaseStatementHandler#BaseStatementHandler
org.apache.ibatis.session.Configuration#newParameterHandler
org.apache.ibatis.plugin.InterceptorChain#pluginAll(parameterHandler)
org.apache.ibatis.session.Configuration#newResultSetHandler
org.apache.ibatis.plugin.InterceptorChain#pluginAll(resultSetHandler)
>org.apache.ibatis.plugin.InterceptorChain#pluginAll(statementHandler)
>org.apache.ibatis.executor.BaseExecutor#getConnection
>org.apache.ibatis.executor.statement.PreparedStatementHandler#instantiateStatement
>org.apache.ibatis.executor.statement.PreparedStatementHandler#parameterize
>org.apache.ibatis.scripting.defaults.DefaultParameterHandler#setParameters
org.apache.ibatis.type.BaseTypeHandler#setParameter
org.apache.ibatis.type.UnknownTypeHandler#setNonNullParameter
org.apache.ibatis.type.IntegerTypeHandler#setNonNullParameter