在 mybatis源码分析-配置文件解析过程 一文中,主要对配置文件解析过程作了讲解,而且针对插件的配置文件作了详细讲解,本文主要对 mapper 映射文件的解析进行讲解。segmentfault
首先咱们看下mybatis官网,看下mapper文件如何被全局配置文件加载的。下面是官网的说明:缓存
既然 MyBatis 的行为已经由上述元素配置完了,咱们如今就要定义 SQL 映射语句了。 可是首先咱们须要告诉 MyBatis 到哪里去找到这些语句。 Java 在自动查找这方面没有提供一个很好的方法,因此最佳的方式是告诉 MyBatis 到哪里去找映射文件。 你可使用相对于类路径的资源引用, 或彻底限定资源定位符(包括file:///的 URL),或类名和包名等。例如:mybatis
<!-- 使用相对于类路径的资源引用 --> <mappers> <mapper resource="org/mybatis/builder/AuthorMapper.xml"/> <mapper resource="org/mybatis/builder/BlogMapper.xml"/> <mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers>
<!-- 使用彻底限定资源定位符(URL) --> <mappers> <mapper url="file:///var/mappers/AuthorMapper.xml"/> <mapper url="file:///var/mappers/BlogMapper.xml"/> <mapper url="file:///var/mappers/PostMapper.xml"/> </mappers>
<!-- 使用映射器接口实现类的彻底限定类名 --> <mappers> <mapper class="org.mybatis.builder.AuthorMapper"/> <mapper class="org.mybatis.builder.BlogMapper"/> <mapper class="org.mybatis.builder.PostMapper"/> </mappers>
<!-- 将包内的映射器接口实现所有注册为映射器 --> <mappers> <package name="org.mybatis.builder"/> </mappers>
上面是mybatis所支持的mapper文件的编写方式,咱们下面从源码角度研究一下。app
首先了解一点,咱们须要把接口所有加载到 MapperRegistry
类中,使用map保存。源码分析
private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
这个 MapperRegistry
类暂时不讲,之后会详细讲解。只要知道 Configuration
类有 addMapper
方法能够把接口加入到上面的 map 中便可。下面看具体源码:ui
private void mapperElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { if ("package".equals(child.getName())) { //① String mapperPackage = child.getStringAttribute("name"); configuration.addMappers(mapperPackage); } else { String resource = child.getStringAttribute("resource"); String url = child.getStringAttribute("url"); String mapperClass = child.getStringAttribute("class"); if (resource != null && url == null && mapperClass == null) { //② ErrorContext.instance().resource(resource); InputStream inputStream = Resources.getResourceAsStream(resource); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url != null && mapperClass == null) { //③ ErrorContext.instance().resource(url); InputStream inputStream = Resources.getUrlAsStream(url); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } 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."); } } } } }
XMLMapperBuilder
对象,而不是 XMLConfigBuilder
对象。XMLMapperBuilder
对象的parse方法就能够进行将mapper文件进行解析。上面无论使用什么方式,都须要在加载全局配置文件的时候,将咱们所定义的接口先缓存起来,以便后面接口的调用。可是上面4种配置方式大体能够分为 2 种,一种直接找 mapper xml 配置文件,一种是找接口或者包(包其实下面也是接口)。url
本节暂时不讲解那么多,后面章节主要讲解这两种方式。不过能够首先透漏下:spa
XMLMapperBuilder
的研究。