hello~各位读者好,我是鸭血粉丝(大家可以称呼我为「阿粉」),在这个特殊的日子里,大家要注意安全,尽量不要出门,无聊的话,就像阿粉一样,把时间愉快的花在学习上吧。
1.上期回顾
前面初识 mybatis
章节,阿粉首先搭建了一个简单的项目,只用了 mybatis
的 jar
包。然后通过一个测试代码,讲解了几个重要的类和步骤。
先看下这个测试类:
1 |
|
这章的话,阿粉会带着大家理解一下源码,基于上面测试的代码。阿粉先申明一下,源码一章肯定是讲不完的,所以阿粉会分成几个章节,并且讲源码的话,代码肯定比较多,比较干,所以请大家事先准备好开水哦。
2.源码分析
这里阿粉会根据 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream)
这段代码来分析 mybatis
做了哪些事情。
2.1设计模式
通过这段代码,我们首先分析一下用到了哪些设计模式呢?
首先 SqlSessionFactory
这个类用到了工厂模式,并且上一章阿粉也说到了,这个类是全局唯一的,所以它还使用了单列模式。
然后是 SqlSessionFactoryBuilder
这个类,一看就知道用到了建造者模式。
所以,只看这段代码就用到了工厂模式,单列模式和建造者模式。
2.2mybatis做了什么事情
这里就开始源码之旅了,首先 ctrl
+ 左键点击 build
方法,我们会看到
1 |
|
再点击 build
,然后就是
1 |
|
首先 XMLConfigBuilder
这个类就是用来解析我们的配置文件 mybatis-config.xml
,调用这个类的构造函数,主要是创建一个 Configuration
对象,这个对象的属性就对应mybatis-config.xml
里面的一级标签。这里不贴代码,有兴趣的自己点开看下。
然后就是 build(parser.parse())
这段代码,先执行 parse()
方法,再执行 build()
方法。不过这里,阿粉先说下 build()
这个方法,首先parse()
方法返回的就是 Configuration
对象,然后我们点击 build
1 |
|
这里就是返回的默认的SqlSessionFactory
。
然后我们再说 parse()
方法,因为这个是核心方法。我们点进去看下
1 |
|
首先是 if
判断,就是防止解析多次。看 parseConfiguration
方法
1 |
|
这里就开始了解析mybatis-config.xml
里面的一级标签了。阿粉不全部讲,只说几个主要的。
- settings:全局配置,比如我们的二级缓存,延迟加载,日志打印等
- mappers:解析dao层的接口和mapper.xml文件
- properties:这个一般是配置数据库的信息,如:数据库连接,用户名和密码等,不讲
- typeAliases :参数和返回结果的实体类的别名,不讲
- plugins:插件,如:分页插件,不讲
- objectFactory,objectWrapperFactory:实例化对象用的,比如返回结果是一个对象,就是通过这个工厂反射获取的,不讲
- environments:事物和数据源的配置,不讲
- databaseIdProvider:这个是用来支持不同厂商的数据库
- typeHandlers:这个是java类型和数据库的类型做映射,比如数据库的
varchar
类型对应java
的String
类型,不讲
2.3解析settings
我们点击 settingsElement(settings)
这个方法
1 |
|
这个就是设置我们的全局配置信息, setXXX
方法的第一个参数是 mybatis-config.xml
里面
2.4解析mappers
点击 mapperElement
方法
1 |
|
这里有4个判断 page (包),resource(相对路径,阿粉配的就是这个,所有按照这个讲解源码),url(绝对路径),class(单个接口)
XMLMapperBuilder
这个类是用来解析mapper.xml文件的。然后我们点击 parse
方法
1 |
|
if 判断是判断 mapper
是不是已经注册了,单个Mapper重复注册会抛出异常。
-
configurationElement:解析 mapper 里面所有子标签
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17private void configurationElement(XNode context) { try { String namespace = context.getStringAttribute("namespace"); if (namespace == null || namespace.equals("")) { throw new BuilderException("Mapper's namespace cannot be empty"); } builderAssistant.setCurrentNamespace(namespace); cacheRefElement(context.evalNode("cache-ref")); cacheElement(context.evalNode("cache")); parameterMapElement(context.evalNodes("/mapper/parameterMap")); resultMapElements(context.evalNodes("/mapper/resultMap")); sqlElement(context.evalNodes("/mapper/sql")); buildStatementFromContext(context.evalNodes("select|insert|update|delete")); } catch (Exception e) { throw new BuilderException("Error parsing Mapper XML. The XML location is '" + resource + "'. Cause: " + e, e); } }
cacheRefElement:缓存
cacheElement:是否开启二级缓存
parameterMapElement:配置的参数映射
resultMapElements:配置的结果映射
sqlElement:公用sql配置
buildStatementFromContext:解析
select|insert|update|delete
标签,获得 MappedStatement 对象,一个标签一个对象 -
bindMapperForNamespace:把namespace对应的接口的类型和代理工厂类绑定起来。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20private void bindMapperForNamespace() { String namespace = builderAssistant.getCurrentNamespace(); if (namespace != null) { Class<?> boundType = null; try { boundType = Resources.classForName(namespace); } catch (ClassNotFoundException e) { //ignore, bound type is not required } if (boundType != null) { if (!configuration.hasMapper(boundType)) { // Spring may not know the real resource name so we set a flag // to prevent loading again this resource from the mapper interface // look at MapperAnnotationBuilder#loadXmlResource configuration.addLoadedResource("namespace:" + namespace); configuration.addMapper(boundType); } } } }
看最后
addMapper
方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public <T> void addMapper(Class<T> type) { if (type.isInterface()) { if (hasMapper(type)) { throw new BindingException("Type " + type + " is already known to the MapperRegistry."); } boolean loadCompleted = false; try { knownMappers.put(type, new MapperProxyFactory<>(type)); // It's important that the type is added before the parser is run // otherwise the binding may automatically be attempted by the // mapper parser. If the type is already known, it won't try. MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); parser.parse(); loadCompleted = true; } finally { if (!loadCompleted) { knownMappers.remove(type); } } } }
判断
namespace
对应的类是否是接口,然后判断是否已经加载了这个接口,最后将namespace
对应的接口和MapperProxyFactory
放到 map 容器中。MapperProxyFactory
工厂模式,创建MapperProxy
对象,这个一看就是代理对象。这个就是根据接口里面的方法获取mapper.xml
里面对应的sql语句的关键所在。具体的等下次讲解getMapper()源码的时候在深入的讲解。
3.时序图
4.总结
在这一步,我们主要完成了 config
配置文件、Mapper
文件、Mapper
接口解析。我们得到了一个最重要的对象Configuration
,这里面存放了全部的配置信息,它在属性里面还有各种各样的容器。最后,返回了一个DefaultSqlSessionFactory
,里面持有了 Configuration
的实例。