mybatis源码分析-映射文件解析

mybatis源码分析-配置文件解析过程 一文中,主要对配置文件解析过程做了讲解,并且针对插件的配置文件做了详细讲解,本文主要对 mapper 映射文件的解析进行讲解。

mapper文件的加载

首先我们看下mybatis官网,看下mapper文件如何被全局配置文件加载的。下面是官网的说明:

既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了。 但是首先我们需要告诉 MyBatis 到哪里去找到这些语句。 Java 在自动查找这方面没有提供一个很好的方法,所以最佳的方式是告诉 MyBatis 到哪里去找映射文件。 你可以使用相对于类路径的资源引用, 或完全限定资源定位符(包括file:///的 URL),或类名和包名等。例如:

<!-- 使用相对于类路径的资源引用 -->  
<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文件的编写方式,我们下面从源码角度研究一下。

mapper文件解析源码

首先了解一点,我们需要把接口全部加载到 MapperRegistry 类中,使用map保存。

private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

这个 MapperRegistry 类暂时不讲,以后会详细讲解。只要知道 Configuration 类有 addMapper 方法可以把接口加入到上面的 map 中即可。下面看具体源码:

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.");  
                }  
            }  
        }  
    }  
}
  • ①如果配置文件使用的是使用 package 方式,需要把该包下面的所有接口都存入 map 中。
  • ②如果配置是resource方式,则获取到 mapper 映射文件的路径,然后解析这些文件,注意解析生成的是 XMLMapperBuilder 对象,而不是 XMLConfigBuilder 对象。XMLMapperBuilder 对象的parse方法就可以进行将mapper文件进行解析。
  • ③如果配置是url方式,同 resource。
  • ④如果配置的是 class ,那么直接存入 map ,无需解析。这种方式使用 mapper 接口替代 mapper映射文件的配置方式。

上面不管使用什么方式,都需要在加载全局配置文件的时候,将我们所定义的接口先缓存起来,以便后面接口的调用。但是上面4种配置方式大致可以分为 2 种,一种直接找 mapper xml 配置文件,一种是找接口或者包(包其实下面也是接口)。

本节暂时不讲解那么多,后面章节主要讲解这两种方式。不过可以首先透漏下:

  • 对于 接口或者包 的形式,主要讲解的代码是

  • 对于其它两种方式
    主要是 XMLMapperBuilder 的研究。

作者:指尖改变世界

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ELvis kin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值