mybatis源码-解析配置文件(三)之配置文件Configuration解析 中, 讲解了 Configuration 是如何解析的。

其中, mappers作为configuration节点的一部分配置, 在本文章中, 我们讲解解析mappers节点, 即 xxxMapper.xml 文件的解析。

1 解析入口

在解析 mybatis-config.xml 时, 会进行解析 xxxMapper.xml 的文件。



在图示流程的 XMLConfigBuilder.parse() 函数中, 该函数内部, 在解析 mappers 节点时, 会调用 mapperElement(root.evalNode("mappers"))

private void mapperElement(XNode parent) throws Exception {
if (parent != null) {
// 遍历其子节点
for (XNode child : parent.getChildren()) {
// 如果配置的是包(packege)
if ("package".equals(child.getName())) {
String mapperPackage = child.getStringAttribute("name");
configuration.addMappers(mapperPackage);
} else {
// 如果配置的是类(有三种情况 resource / class / url)
String resource = child.getStringAttribute("resource");
String url = child.getStringAttribute("url");
String mapperClass = child.getStringAttribute("class");
// 配置一:使用 resource 类路径
if (resource != null && url == null && mapperClass == null) {
ErrorContext.instance().resource(resource);
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建 XMLMapperBuilder 对象
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
// 解析 xxxMapper.xml
mapperParser.parse();
// 配置二: 使用 url 绝对路径
} else if (resource == null && url != null && mapperClass == null) {
ErrorContext.instance().resource(url);
InputStream inputStream = Resources.getUrlAsStream(url);
// 创建 XMLMapperBuilder 对象
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
// 解析 xxxMapper.xml
mapperParser.parse();
// 配置三: 使用 class 类名
} else if (resource == null && url == null && mapperClass != null) {
// 通过反射创建对象
Class<?> mapperInterface = Resources.classForName(mapperClass);
// 添加
configuration.addMapper(mapperInterface);
} else {
throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
}
}
}
}
}

从以上源码中可以发现, 配置时, 一种是通过包的方式, 一种是通过指定文件的方式。

但不管是怎么配置, 最后的找落点都是 xxxMapper.xml 文件的解析。

2 解析

包扫描时, 会加载指定包下的文件, 最终会调用

private void loadXmlResource() {
// 判断是否已经加载过
if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
String xmlResource = type.getName().replace('.', '/') + ".xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
} catch (IOException e) {
// ignore, resource is not required
}
if (inputStream != null) {
XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
// 解析
xmlParser.parse();
}
}
}

因此, 不管是包扫描还是文件扫描, 最终都经历一下 xmlParser.parse() 解析过程。

2.1 解析流程

解析 xxxMapper.xml 文件的是下面这个函数,解析 mapper 节点。

  public void parse() {
// 判断是否已经加载过
if (!configuration.isResourceLoaded(resource)) {
// 解析 <mapper> 节点
configurationElement(parser.evalNode("/mapper"));
// 标记一下,已经加载过了
configuration.addLoadedResource(resource);
// 绑定映射器到namespace
bindMapperForNamespace();
}
// 处理 configurationElement 中解析失败的<resultMap>
parsePendingResultMaps();
// 处理configurationElement 中解析失败的<cache-ref>
parsePendingCacheRefs();
// 处理 configurationElement 中解析失败的 SQL 语句
parsePendingStatements();
}

大致流程

  1. 解析调用 configurationElement() 函数来解析各个节点
  2. 标记传入的文件已经解析了
  3. 绑定文件到相应的 namespace, 所以 namespace 需要是唯一的
  4. 处理解析失败的节点

2.2 解析各个节点

  private void configurationElement(XNode context) {
try {
// 获取namespace属性, 其代表者这个文档的标识
String namespace = context.getStringAttribute("namespace");
if (namespace == null || namespace.equals("")) {
throw new BuilderException("Mapper's namespace cannot be empty");
}
builderAssistant.setCurrentNamespace(namespace);
// 解析 <cache-ref> 节点
cacheRefElement(context.evalNode("cache-ref"));
// 解析 <cache> 节点
cacheElement(context.evalNode("cache"));
// 解析 </mapper/parameterMap> 节点
parameterMapElement(context.evalNodes("/mapper/parameterMap"));
// 解析 </mapper/resultMap> 节点
resultMapElements(context.evalNodes("/mapper/resultMap"));
// 解析 </mapper/sql> 节点
sqlElement(context.evalNodes("/mapper/sql"));
// 解析 select|insert|update|delet 节点
buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
} catch (Exception e) {
throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
}
}

为了避免篇幅太长, 在此就不深入讲解各个解析过程, 后续会开专门的章节。

一起学 mybatis

你想不想来学习 mybatis? 学习其使用和源码呢?那么, 在博客园关注我吧!!

我自己打算把这个源码系列更新完毕, 同时会更新相应的注释。快去 star 吧!!

mybatis最新源码和注释

mybatis源码-解析配置文件(四)之配置文件Mapper解析的更多相关文章

  1. Mybatis源码详解系列(三)--从Mapper接口开始看Mybatis的执行逻辑

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  2. mybatis源码分析(二)------------配置文件的解析

    这篇文章中,我们将讲解配置文件中 properties,typeAliases,settings和environments这些节点的解析过程. 一 properties的解析 private void ...

  3. mybatis源码学习(四):动态SQL的解析

    之前的一片文章中我们已经了解了MappedStatement中有一个SqlSource字段,而SqlSource又有一个getBoundSql方法来获得BoundSql对象.而BoundSql中的sq ...

  4. mybatis源码分析(四)---------------代理对象的生成

    在mybatis两种开发方式这边文章中,我们提到了Mapper动态代理开发这种方式,现在抛出一个问题:通过sqlSession.getMapper(XXXMapper.class)来获取代理对象的过程 ...

  5. MyBatis源码分析(四):SQL执行过程分析

    一.获取Mapper接口的代理 根据上一节,Mybatis初始化之后,利用sqlSession(defaultSqlSession)的getMapper方法获取Mapper接口 1 @Override ...

  6. mybatis 源码分析(四)一二级缓存分析

    本篇博客主要讲了 mybatis 一二级缓存的构成,以及一些容易出错地方的示例分析: 一.mybatis 缓存体系 mybatis 的一二级缓存体系大致如下: 首先当一二级缓存同时开启的时候,首先命中 ...

  7. Mybatis源码解读-配置加载和Mapper的生成

    问题 Mybatis四大对象的创建顺序? Mybatis插件的执行顺序? 工程创建 环境:Mybatis(3.5.9) mybatis-demo,参考官方文档 简单示例 这里只放出main方法的示例, ...

  8. mybatis源码探索笔记-3(使用代理mapper执行方法)

    前言 前面两章我们构建了SqlSessionFactory,并通过SqlSessionFactory创建了我们需要的SqlSession,并通过这个SqlSession获取了我们需要的代理mapper ...

  9. mybatis源码学习(一):Mapper的绑定

    在mybatis中,我们可以像下面这样通过声明对应的接口来绑定XML中的mapper,这样可以让我们尽早的发现XML的错误. 定义XML: <?xml version="1.0&quo ...

  10. 【mybatis源码学习】与spring整合Mapper接口执行原理

    一.重要的接口 org.mybatis.spring.mapper.MapperFactoryBean MapperScannerConfigurer会向spring中注册该bean,一个mapper ...

随机推荐

  1. tinypng upload一键压缩上传工具,告别人肉

    地址 项目地址:tinypng-upload 有兴趣的可以玩一玩,因为平时经常会用到图片压缩,上传,如果你也觉得很繁琐的话,这个将会解决你的痛点. 关于 tinypng-upload 这是一个基于 e ...

  2. URL编码:怎样读取特殊字符

    URL编码:怎样读取特殊字符 (这个我曾经谢过教程,这里整理过来)从外部文本载入到动态文本的时候,一些特殊字符(如&/%等)无法正常现实,有的符号还会导致这个符号后面的字符无法现实(如& ...

  3. virtualenv 的使用

    首先,我们用pip安装virtualenv: 一.使用与启动: $ pip3 install virtualenv 然后,假定我们要开发一个新的项目,需要一套独立的Python运行环境,可以这么做: ...

  4. 给电脑插上无线网卡,变成路由器----Windows系统承载网络的使用

    1. 以管理员身份运行命令提示符(PowerShell) 2. 启用并设定虚拟wifi网卡 netsh wlan set hostednetwork mode=allow ssid=wifi名称 ke ...

  5. Django电商项目---完成登录验证和用户中心(个人信息)day3

    登录验证的实现 背景说明: 用户在商品界面选择商品后,在点击购物车或者结算订单之前 需要完成用户的登录验证,这里用装饰器来完成   创建装饰器类: df_user/user_decorator.py ...

  6. Kubernetes的搭建与配置(一):集群环境搭建

    1.环境介绍及准备: 1.1 物理机操作系统 物理机操作系统采用Centos7.3 64位,细节如下. [root@localhost ~]# uname -a Linux localhost.loc ...

  7. 第3章 Git使用人门

    [初识Github] 首先让我们大家一起喊一句“Hello Github”.YEAH!就是这样. Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理 ...

  8. localStorage 知识点

    先补充下localStorage 知识点:JS对象读取形式:localStorage.name添加/修改localStorage.name = "xuanyuan"其中" ...

  9. css 位置居中篇,flex布局【转】

    最近看到沅老师的博客,关于flex布局的,觉得不错,http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

  10. 网络流(四)dinic算法

    传送门: 网络流(一)基础知识篇 网络流(二)最大流的增广路算法 网络流(三)最大流最小割定理 网络流(四)dinic算法 网络流(五)有上下限的最大流 网络流(六)最小费用最大流问题 转自:http ...