在 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();
- }
大致流程:
解析调用
configurationElement()
函数来解析各个节点
标记传入的文件已经解析了
绑定文件到相应的 namespace, 所以 namespace 需要是唯一的
处理解析失败的节点
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);
- }
- }
为了避免篇幅太长, 在此就不深入讲解各个解析过程, 后续会开专门的章节.
来源: https://www.cnblogs.com/homejim/p/9741404.html