Mybatis
2022-04-07 18:35:22 18 举报
MyBatis是一个优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集的过程。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJOs(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。MyBatis的主要特点包括:简单易用、灵活、可插拔、提供XML标签,解除sql与程序代码的耦合,提供映射标签,支持对象与数据库的ORM字段关系映射,提供对象关系映射标签,支持对象关系组件维护复杂对象。
作者其他创作
大纲/内容
SqlSource
#isDynamic
$Proxy#MapperProxy
#createParser -> new GenericTokenParser(“${”,“},DynamicCheckerTokenParser)#parse -> 验证sql是否存在${}
false
MapperMethod#execute
XMLLanguageDriver#createSqlSource
RawSqlSource
bindMapperForNamespace
返回MapperProxy代理类
mapperElement
Configuration
二级缓存缓存查询结果
BaseExecutor
convertArgsToSqlCommandParam
instantiateStatement(connection)
处理二级缓存
tx.getConnection
CacheKey
invoke
openSession
DynamicSqlSource
BoundSql
get
解析/configuration标签
返回
Configuration#interceptorChain#interceptors -> add
new MapperMethod
DefaultResultSetHandler#handleResultSets(stmt)
MapperMethod == null
命中
ms.statementType默认为prepare
PreparedStatementHandler
sqlparameterMappings -> key:参数位置,value:参数名称、类型...parameterObject -> key:参数名称,value:参数值
getMapper
ResultSetHandler
configurationElement
mappedStatementparameterrowBoundsboundSql
returnXXX、rowBoundsIndex、resultHandlerIndex、ParamNameResolver
Configuration#mapperRegistry#getMapper(type,this)this -> sqlSession
XMLStatementBuilder#parseStatementNode
true
doQuery
parseDynamicTags
二级缓存是namespace内部对应<cache/>
name、typename -> ms.idtype -> ms.sqlCommandType
XPathParser解析Config.xml
MappedStatement - ms
TransactionFactory
new DefaultSqlSessionFactory(configuration)
GenericTokenParser#parse
openSessionFromDataSource
confguration#newStatementHandler
SimpleExecutor
责任链模式
StaticTextSqlNode
SimpleStatementHandler
isDynamic
解析namespace属性、cache、sql、resultmap等标签
按照sqlCommand的name获取MappedStatement
configuration.getEnvironment()
queryFromDatabase
是否开启二级缓存
构建MappedStatement
DefaultSqlSession#update | selectList
BaseExecutor#query
Configuration#addMapper
XPathParser#inputStream -> document
解析select|insert|update|delete标签
PreparedStatement#execute
SqlSessionFactory
mapperInterface、method、sqlSession.getConfiguration()
SqlSession
mapperRegistry#addMapper
SqlCommand
ParameterHandler
处理一级缓存
RoutingStatementHandler#query(stmt)
TextSqlNode
newTransaction
MappedStatement
RoutingStatementHandler#parameterize(stmt)
ParameterMappingTokenHandler#handleToken
MapperProxyFactory
一级缓存缓存查询结果
MethodSignature
解析配置plugins
CallableStatementHandler
Executor
StatementHandler
按照sqlCommand的type区分调用修改或查询
XMLScriptBuilder#parseScriptNode
StaticSqlSource
XPathParser解析Mapper.xml
SqlSourceBuilder#parse
Transaction
knownMappers.get(type)
MappedStatement#getBoundSql(param)
解析配置mappers
装饰者模式
ParamNameResolver
RoutingStatementHandler
sqlparameterMappingsconfiguration
CachingExecutor#query
RoutingStatementHandler#prepare(connection)
SqlSessionFactoryBuilder#build
#new GenericTokenParser(“#{”,“},ParameterMappingTokenHandler)#parse -> 验证sql是否存在#{}
Congiguration#interceptorChain#interceptors#interceptor#plugin
buildStatementFromContext
jdk动态代理
param
将#{} -> ?parameterMappings
parseConfiguration
调用父类BaseBuilder#new Configuration
configuration#interceptorChain#interceptors#interceptor.plugin
DB
ReuseExecutor
未命中
configuration.newExecutor(tx,SIMPLE)
mappedStatement.getConfiguration
XMLConfigBuilder#parse
XMLMapperBuilder#parse
Statement -> PreparedStatement
put
BatchExecutor
一级缓存PerpetualCache localCache
模板模式
解析mapper标签
CachingExecutor
pluginElement
new DefaultSqlSession(configuration,executor,autoCommit)
${}在这里就已经完成了值替换
MapperProxy implements InvocationHandler
MixedSqlNode
0 条评论
下一页