mybatis源码分析
2020-04-23 16:17:42 105 举报
AI智能生成
mybatis源码分析
作者其他创作
大纲/内容
获取SqlSessionFactory对象
new SqlSessionFactoryBuilder()
执行build方法,
返回SqlSessionFactory对象
返回SqlSessionFactory对象
XMLConfigBuilder parser =
new XMLConfigBuilder(inputStream, environment, properties);
创建XMLConfigBuilder对象,并初始化字段
new XMLConfigBuilder(inputStream, environment, properties);
创建XMLConfigBuilder对象,并初始化字段
this.configuration = configuration;
this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
this.environment = environment;
this.parser =
new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()
创建XPathParser解析器
new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()
创建XPathParser解析器
parser.parse()
解析全局配置文件
解析全局配置文件
parseConfiguration
propertiesElement
解析<properties>元素
解析<properties>元素
Properties defaults = context.getChildrenAsProperties();
添加子元素内容至defaults
添加子元素内容至defaults
defaults =Resources.getResourceAsProperties(resource)
读取外部properties文件内容至defaults
读取外部properties文件内容至defaults
parser.setVariables(defaults);
XPathParser添加变量信息
XPathParser添加变量信息
configuration.setVariables(defaults)
settingsAsProperties
解析<settings>元素
mybatis全局设置
解析<settings>元素
mybatis全局设置
Properties props = context.getChildrenAsProperties();
获取子元素内容
获取子元素内容
metaConfig.hasSetter(String.valueOf(key)
校验子元素是否合法
校验子元素是否合法
typeAliasesElement
解析<typeAliases>元素
类型别名
解析<typeAliases>元素
类型别名
pluginElement
解析<plugIns>元素
插件
解析<plugIns>元素
插件
获取拦截器
child.getStringAttribute("interceptor");
child.getStringAttribute("interceptor");
获取属性
child.getChildrenAsProperties()
child.getChildrenAsProperties()
创建拦截器实例
Interceptor interceptorInstance =
(Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();
Interceptor interceptorInstance =
(Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();
添加拦截器至configuration全局变量中
configuration.addInterceptor(interceptorInstance)
configuration.addInterceptor(interceptorInstance)
objectFactoryElement
解析<objectFactory>元素
解析<objectFactory>元素
objectWrapperFactoryElement
解析<objectWrapperFactory>元素
解析<objectWrapperFactory>元素
reflectorFactoryElement
解析<reflectorFactory>元素
解析<reflectorFactory>元素
settingsElement
将<settings>元素内容填充至configuration对象中
将<settings>元素内容填充至configuration对象中
将<settings>元素配置的全局属性添加至configuration中
configuration.setCacheEnabled(booleanValueOf(props.getProperty("cacheEnabled"), true));
configuration.setMapUnderscoreToCamelCase(booleanValueOf(props.getProperty("mapUnderscoreToCamelCase"), false));
....
databaseIdProviderElement
解析<databaseIdProvider>元素
数据库厂商标识
解析<databaseIdProvider>元素
数据库厂商标识
typeHandlerElement
解析<typeHandlers>元素
解析<typeHandlers>元素
mapperElement
解析<mappers>元素
解析<mappers>元素
package
configuration.addMappers(mapperPackage)
MapperRegistry->
addMappers(packageName);
addMappers(packageName);
resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
解析指定包中Class
解析指定包中Class
addMapper(mapperClass);
knownMappers.put(type, new MapperProxyFactory<>(type));
添加MapperProxy代理对象
添加MapperProxy代理对象
MapperAnnotationBuilder->parse()
解析Mapper接口中定义的注解
解析Mapper接口中定义的注解
MapperAnnotationBuilder->
loadXmlResource();
加载xml映射文件
loadXmlResource();
加载xml映射文件
InputStream inputStream = type.getResourceAsStream("/" + xmlResource);
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
xmlParser.parse();
xmlParser.parse();
MapperAnnotationBuilder->
parseStatement(method);
parseStatement(method);
MapperBuilderAssistant->
addMappedStatement
addMappedStatement
mapper
resource
url
url
Resources.getResourceAsStream(resource)
加载资源文件为流
加载资源文件为流
XMLMapperBuilder->
parse()
解析<mapper>元素
parse()
解析<mapper>元素
configurationElement
namespace
cacheRefElement
解析cache-ref属性
解析cache-ref属性
cacheElement
解析cache属性
解析cache属性
parameterMapElement
/mapper/parameterMap
/mapper/parameterMap
builderAssistant.addParameterMap
resultMapElements
/mapper/resultMap
/mapper/resultMap
sqlElement
/mapper/sql
/mapper/sql
buildStatementFromContext
select|insert|update|delete
select|insert|update|delete
XMLStatementBuilder->
parseStatementNode();
parseStatementNode();
解析xml元素
MapperBuilderAssistant->
addMappedStatement
addMappedStatement
MappedStatement statement = statementBuilder.build();
configuration.addMappedStatement(statement);
bindMapperForNamespace
builderAssistant.getCurrentNamespace()
configuration.addLoadedResource("namespace:" + namespace);
configuration.addMapper(boundType);
parsePendingResultMaps
parsePendingCacheRefs
parsePendingStatements
class
Resources.classForName(mapperClass);
configuration.addMapper(boundType);
获取SqlSession对象
DefaultSqlSessionFactory->
openSession()
openSession()
DefaultSqlSessionFactory->
openSessionFromDataSource
openSessionFromDataSource
configuration.getEnvironment()
获取环境变量
获取环境变量
tx = transactionFactory.newTransaction();
创建事务
创建事务
Executor executor = configuration.newExecutor(tx, execType)
创建执行器
创建执行器
BatchExecutor
执行器不仅重用语句还会执行批量更新
执行器不仅重用语句还会执行批量更新
ReuseExecutor
执行器会重用预处理语句(PreparedStatement)
执行器会重用预处理语句(PreparedStatement)
SimpleExecutor
普通的执行器
普通的执行器
CachingExecutor
setting中开启二级缓存(cacheEnabled=true)
setting中开启二级缓存(cacheEnabled=true)
executor = (Executor) interceptorChain.pluginAll(executor);
拦截器处理,插件对执行器进行二次包装
拦截器处理,插件对执行器进行二次包装
new DefaultSqlSession(configuration, executor, autoCommit);
返回DefaultSqlSession对象
返回DefaultSqlSession对象
获取接口代理对象 MapperProxy
DefaultSqlSession->
getMapper(Class<T> type)
getMapper(Class<T> type)
Configuration->
getMapper
getMapper
MapperRegistry->
getMapper
getMapper
(MapperProxyFactory<T>) knownMappers.get(type)
mapperProxyFactory.newInstance(sqlSession)
MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
(T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
执行增删改查方法
MapperProxy->
invoke()
进入代理对象invoke方法
invoke()
进入代理对象invoke方法
Object上定义的方法直接执行
MapperMethod->
execute
execute
command.getType()
INSERT|UPDATE|DELETE|SELECT
INSERT|UPDATE|DELETE|SELECT
executeForMany
method.convertArgsToSqlCommandParam
paramNameResolver.getNamedParams(args)
sqlSession.selectList(command.getName(), param)
DefaultSqlSession->
configuration.getMappedStatement(statement)
configuration.getMappedStatement(statement)
wrapCollection()
参数统一封装为map
参数统一封装为map
executor.query
执行器查询
执行器查询
MappedStatement->
ms.getBoundSql(parameterObject)
ms.getBoundSql(parameterObject)
query
先查询缓存
delegate.query
BaseExecutor->
queryFromDatabase
queryFromDatabase
doQuery
ms.getConfiguration();
StatementHandler handler =
configuration.newStatementHandler();
configuration.newStatementHandler();
StatementHandler statementHandler =
new RoutingStatementHandler
new RoutingStatementHandler
STATEMENT:SimpleStatementHandler
PREPARED:PreparedStatementHandler
CALLABLE:CallableStatementHandler
BaseStatementHandler->
this.parameterHandler = configuration.newParameterHandler
创建参数处理器
创建参数处理器
(ParameterHandler) interceptorChain.pluginAll(parameterHandler);
this.resultSetHandler = configuration.newResultSetHandler
创建结果处理器
创建结果处理器
resultSetHandler = (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);
(StatementHandler) interceptorChain.pluginAll(statementHandler);
stmt = prepareStatement(handler, ms.getStatementLog());
Connection connection = getConnection(statementLog);
JdbcTransaction->
transaction.getConnection();
获取数据库连接
transaction.getConnection();
获取数据库连接
dataSource.getConnection()
从连接池获取connection
从连接池获取connection
connection.setAutoCommit(desiredAutoCommit);
设置事务
设置事务
stmt = handler.prepare(connection, transaction.getTimeout());
RoutingStatementHandler->
delegate.prepare(connection, transactionTimeout);
delegate.prepare(connection, transactionTimeout);
BaseStatementHandler->
prepare
prepare
instantiateStatement
PreparedStatementHandler->
String sql = boundSql.getSql()
connection.prepareStatement(sql)
String sql = boundSql.getSql()
connection.prepareStatement(sql)
setStatementTimeout
setFetchSize
return statement
handler.parameterize(stmt);
RoutingStatementHandler->
delegate.parameterize(statement);
delegate.parameterize(statement);
PreparedStatementHandler->
parameterHandler.setParameters((PreparedStatement) statement);
parameterHandler.setParameters((PreparedStatement) statement);
DefaultParameterHandler->
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
TypeHandler typeHandler = parameterMapping.getTypeHandler();
JdbcType jdbcType = parameterMapping.getJdbcType();
typeHandler.setParameter(ps, i + 1, value, jdbcType);
handler.query(stmt, resultHandler);
RoutingStatementHandler->
delegate.query(statement, resultHandler);
delegate.query(statement, resultHandler);
PreparedStatementHandler->
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
resultSetHandler.handleResultSets(ps);
处理参数
处理参数
0 条评论
下一页