Mybatis配置之<mappers>元素详述

来源:互联网 发布:数据分析报告的结尾 编辑:程序博客网 时间:2024/05/17 00:06

原文:http://blog.csdn.net/majinggogogo/article/details/72085696


在前面的若干篇文章中,我们已经对mybatis中主要的配置元素做了讲述,还剩下一个比较重要的元素,那就是<mappers>元素。

这个元素是干嘛用的呢?<mappers>用来在mybatis初始化的时候,告诉mybatis需要引入哪些Mapper映射文件。那什么又是Mapper映射文件呢?它是Java实体类与数据库对象之间的桥梁。在实际的使用过程中,一般一个Mapper文件对应一个数据库操作Dao接口。


在mybatis中,mappers必须配置!那么怎么配置呢?我们先从源码上看下这个节点是怎么解析的。

[java] view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;">private void mapperElement(XNode parent) throws Exception {  
  2.     if (parent != null) {  
  3.       for (XNode child : parent.getChildren()) {  
  4.         if ("package".equals(child.getName())) {  
  5.           String mapperPackage = child.getStringAttribute("name");  
  6.           configuration.addMappers(mapperPackage);  
  7.         } else {  
  8.           String resource = child.getStringAttribute("resource");  
  9.           String url = child.getStringAttribute("url");  
  10.           String mapperClass = child.getStringAttribute("class");  
  11.           if (resource != null && url == null && mapperClass == null) {  
  12.             ErrorContext.instance().resource(resource);  
  13.             InputStream inputStream = Resources.getResourceAsStream(resource);  
  14.             XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());  
  15.             mapperParser.parse();  
  16.           } else if (resource == null && url != null && mapperClass == null) {  
  17.             ErrorContext.instance().resource(url);  
  18.             InputStream inputStream = Resources.getUrlAsStream(url);  
  19.             XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());  
  20.             mapperParser.parse();  
  21.           } else if (resource == null && url == null && mapperClass != null) {  
  22.             Class<?> mapperInterface = Resources.classForName(mapperClass);  
  23.             configuration.addMapper(mapperInterface);  
  24.           } else {  
  25.             throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");  
  26.           }  
  27.         }  
  28.       }  
  29.     }  
  30.   }</span>  

上面的代码便是Mybatis中<mappers>节点的解析入口了,我们来简单看下。

从代码看,我们知道分为两大类,一种是配置<package>子元素,另一种是配置<mapper>元素(从http://mybatis.org/dtd/mybatis-3-config.dtd文件限制的)。如下截图所示,


这两种方式都是怎么配置呢?<package>配置很简单,只需要配置一个name属性用于设置包名。<mapper>的配置则比较多样化,支持三种属性方式设置,分别为resource、url和class三个属性。具体的配置如下所示:

[html] view plain copy
  1. <span style="font-family:KaiTi_GB2312;font-size:18px;"><configuration>  
  2.     ......  
  3.     <mappers>  
  4.       <!-- 第一种方式:通过resource指定 -->  
  5.     <mapper resource="com/dy/dao/userDao.xml"/>  
  6.       
  7.      <!-- 第二种方式, 通过class指定接口,进而将接口与对应的xml文件形成映射关系  
  8.              不过,使用这种方式必须保证 接口与mapper文件同名(不区分大小写),   
  9.              我这儿接口是UserDao,那么意味着mapper文件为UserDao.xml   
  10.      <mapper class="com.dy.dao.UserDao"/>  
  11.       -->  
  12.         
  13.       <!-- 第三种方式,直接指定包,自动扫描,与方法二同理   
  14.       <package name="com.dy.dao"/>  
  15.       -->  
  16.       <!-- 第四种方式:通过url指定mapper文件位置  
  17.       <mapper url="file://........"/>  
  18.        -->  
  19.   </mappers>  
  20.     ......  
  21.   </configuration></span>  
阅读全文
0 0