avatar

目录
Mybatis简单阅读code

简单阅读code

其他人的例子

深度分析:mybatis的底层实现原理

简单阅读code记录

自己的随手记 单一个mybatis,版本3.5.3

MybatisSetUp.java
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
String resource = "mybatis-config.xml";
InputStream inputStream=null;
SqlSession sqlSession=null;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"mysql");

sqlSession=sqlSessionFactory.openSession();

try{
LockDemoMapper lockDemoMapper=sqlSession.getMapper(LockDemoMapper.class);
LockDemo lockDemo=lockDemoMapper.selectBalance(1);
String resultString=ReflectionToStringBuilder.toString(lockDemo);
logger.info("查询结果={}",resultString);

lockDemoMapper.addBalance(new BigDecimal("1.3"),1);
sqlSession.commit();

}catch (Throwable t){
logger.error("{}",t);
if(sqlSession!=null){
sqlSession.rollback();
}
}
} catch (IOException e) {
logger.error("",e);

} finally{
if(sqlSession!=null){
sqlSession.close();
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
logger.error("",e);
}
}
}

获取SqlSessionFactory

解析配置文件
plaintext
1
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"mysql");
plaintext
1
2
3
4
5
6
7

parseConfiguration:106, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parse:98, XMLConfigBuilder (org.apache.ibatis.builder.xml)
build:78, SqlSessionFactoryBuilder (org.apache.ibatis.session)
build:68, SqlSessionFactoryBuilder (org.apache.ibatis.session)
main:27, MybatisSetUp (cn.demo.stdemo)

构建org.apache.ibatis.session.Configuration
plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private void parseConfiguration(XNode root) {
try {
this.propertiesElement(root.evalNode("properties"));
Properties settings = this.settingsAsProperties(root.evalNode("settings"));
this.loadCustomVfs(settings);
this.loadCustomLogImpl(settings);
this.typeAliasesElement(root.evalNode("typeAliases"));
this.pluginElement(root.evalNode("plugins"));
this.objectFactoryElement(root.evalNode("objectFactory"));
this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
this.settingsElement(settings);
this.environmentsElement(root.evalNode("environments"));
this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
this.typeHandlerElement(root.evalNode("typeHandlers"));
this.mapperElement(root.evalNode("mappers"));
} catch (Exception var3) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
}
}
构建org.apache.ibatis.binding.MapperRegistry
plaintext
1
2
3
4
5
6
mapperElement:362, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parseConfiguration:119, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parse:98, XMLConfigBuilder (org.apache.ibatis.builder.xml)
build:78, SqlSessionFactoryBuilder (org.apache.ibatis.session)
build:68, SqlSessionFactoryBuilder (org.apache.ibatis.session)
main:27, MybatisSetUp (cn.demo.stdemo)
plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private void mapperElement(XNode parent) throws Exception {
if (parent != null) {
Iterator var2 = parent.getChildren().iterator();

while(true) {
while(var2.hasNext()) {
XNode child = (XNode)var2.next();
String resource;
if ("package".equals(child.getName())) {
resource = child.getStringAttribute("name");
this.configuration.addMappers(resource);
} else {
resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
XMLMapperBuilder mapperParser;
InputStream inputStream;
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
inputStream = Resources.getResourceAsStream(resource);
mapperParser = new XMLMapperBuilder(inputStream, this.configuration, resource, this.configuration.getSqlFragments());
mapperParser.parse();
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
inputStream = Resources.getUrlAsStream(url);
mapperParser = new XMLMapperBuilder(inputStream, this.configuration, url, this.configuration.getSqlFragments());
mapperParser.parse();
} else {
if (resource != null || url != null || mapperClass == null) {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}

Class<?> mapperInterface = Resources.classForName(mapperClass);
this.configuration.addMapper(mapperInterface);
}
}
}

return;
}
}
}

plugin
plaintext
1
2
3
4
5
6
7
8
addInterceptor:37, InterceptorChain (org.apache.ibatis.plugin)
addInterceptor:763, Configuration (org.apache.ibatis.session)
pluginElement:190, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parseConfiguration:110, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parse:98, XMLConfigBuilder (org.apache.ibatis.builder.xml)
build:78, SqlSessionFactoryBuilder (org.apache.ibatis.session)
build:68, SqlSessionFactoryBuilder (org.apache.ibatis.session)
main:27, MybatisSetUp (cn.demo.stdemo)
plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void pluginElement(XNode parent) throws Exception {
if (parent != null) {
Iterator var2 = parent.getChildren().iterator();

while(var2.hasNext()) {
XNode child = (XNode)var2.next();
String interceptor = child.getStringAttribute("interceptor");
Properties properties = child.getChildrenAsProperties();
Interceptor interceptorInstance = (Interceptor)this.resolveClass(interceptor).getDeclaredConstructor().newInstance();
interceptorInstance.setProperties(properties);
this.configuration.addInterceptor(interceptorInstance);
}
}

}
初始化
plaintext
1
2
3
4
5
6
7
8
9
<init>:40, Plugin (org.apache.ibatis.plugin)
wrap:49, Plugin (org.apache.ibatis.plugin)
plugin:28, Interceptor (org.apache.ibatis.plugin)
pluginAll:31, InterceptorChain (org.apache.ibatis.plugin)
newExecutor:615, Configuration (org.apache.ibatis.session)
openSessionFromDataSource:96, DefaultSqlSessionFactory (org.apache.ibatis.session.defaults)
openSession:47, DefaultSqlSessionFactory (org.apache.ibatis.session.defaults)
main:29, MybatisSetUp (cn.demo.stdemo)

sqlSession=sqlSessionFactory.openSession();

关于plugin部分
调用到Plugin
plaintext
1
2
3
4
5
6
7
invoke:61, Plugin (org.apache.ibatis.plugin)
update:-1, $Proxy5 (com.sun.proxy)
update:197, DefaultSqlSession (org.apache.ibatis.session.defaults)
execute:67, MapperMethod (org.apache.ibatis.binding)
invoke:93, MapperProxy (org.apache.ibatis.binding)
addBalance:-1, $Proxy6 (com.sun.proxy)
main:37, MybatisSetUp (cn.demo.stdemo)
Plugin 反射 Intercept
plaintext
1
2
3
4
5
6
7
8
intercept:27, ExamplePlugin (cn.demo.stdemo.plugin)
invoke:61, Plugin (org.apache.ibatis.plugin)
update:-1, $Proxy5 (com.sun.proxy)
update:197, DefaultSqlSession (org.apache.ibatis.session.defaults)
execute:67, MapperMethod (org.apache.ibatis.binding)
invoke:93, MapperProxy (org.apache.ibatis.binding)
addBalance:-1, $Proxy6 (com.sun.proxy)
main:37, MybatisSetUp (cn.demo.stdemo)

获取mapper

plaintext
1
2
3
4
5
6
7
8
newInstance:424, Constructor (java.lang.reflect)
newProxyInstance:739, Proxy (java.lang.reflect)
newInstance:47, MapperProxyFactory (org.apache.ibatis.binding)
newInstance:52, MapperProxyFactory (org.apache.ibatis.binding)
getMapper:50, MapperRegistry (org.apache.ibatis.binding)
getMapper:779, Configuration (org.apache.ibatis.session)
getMapper:291, DefaultSqlSession (org.apache.ibatis.session.defaults)
main:32, MybatisSetUp (cn.demo.stdemo)
sql
plaintext
1
2
3
4
5
6
7
8
9
10
11
parseStatementNode:57, XMLStatementBuilder (org.apache.ibatis.builder.xml)
buildStatementFromContext:137, XMLMapperBuilder (org.apache.ibatis.builder.xml)
buildStatementFromContext:130, XMLMapperBuilder (org.apache.ibatis.builder.xml)
configurationElement:120, XMLMapperBuilder (org.apache.ibatis.builder.xml)
parse:94, XMLMapperBuilder (org.apache.ibatis.builder.xml)
mapperElement:374, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parseConfiguration:119, XMLConfigBuilder (org.apache.ibatis.builder.xml)
parse:98, XMLConfigBuilder (org.apache.ibatis.builder.xml)
build:78, SqlSessionFactoryBuilder (org.apache.ibatis.session)
build:68, SqlSessionFactoryBuilder (org.apache.ibatis.session)
main:27, MybatisSetUp (cn.demo.stdemo)
resultHandler
plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
handleResult:43, DefaultResultHandler (org.apache.ibatis.executor.result)
callResultHandler:370, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
storeObject:363, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
handleRowValuesForSimpleResultMap:355, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
handleRowValues:328, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
handleResultSet:301, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
handleResultSets:194, DefaultResultSetHandler (org.apache.ibatis.executor.resultset)
query:65, PreparedStatementHandler (org.apache.ibatis.executor.statement)
query:79, RoutingStatementHandler (org.apache.ibatis.executor.statement)
doQuery:63, SimpleExecutor (org.apache.ibatis.executor)
queryFromDatabase:324, BaseExecutor (org.apache.ibatis.executor)
query:156, BaseExecutor (org.apache.ibatis.executor)
query:109, CachingExecutor (org.apache.ibatis.executor)
query:83, CachingExecutor (org.apache.ibatis.executor)
invoke0:-1, NativeMethodAccessorImpl (sun.reflect)
invoke:62, NativeMethodAccessorImpl (sun.reflect)
invoke:43, DelegatingMethodAccessorImpl (sun.reflect)
invoke:498, Method (java.lang.reflect)
invoke:63, Plugin (org.apache.ibatis.plugin)
query:-1, $Proxy5 (com.sun.proxy)
selectList:147, DefaultSqlSession (org.apache.ibatis.session.defaults)
selectList:140, DefaultSqlSession (org.apache.ibatis.session.defaults)
selectOne:76, DefaultSqlSession (org.apache.ibatis.session.defaults)
execute:87, MapperMethod (org.apache.ibatis.binding)
invoke:93, MapperProxy (org.apache.ibatis.binding)
selectBalance:-1, $Proxy6 (com.sun.proxy)
main:33, MybatisSetUp (cn.demo.stdemo)
文章作者: thf
文章链接: http://pcbopcbo.github.io/2020/07/10/2020071017/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 漂泊的个人笔记

评论