mybatis中<mappers> ,mapperLocations,和MapperScannerConfigurer 用法

x33g5p2x  于2021-12-05 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(327)

1. mappers标签

在mybatis单独使用时,mybatis需要在mybatis-config.xml中配置mappers。mappers 标签下有许多 mapper 标签,每一个 mapper 标签中配置的都是一个独立的映射配置文件的路径,配置方式有以下几种:
1.1 第一种方式:mapper标签,通过resource属性引入classpath路径的相对资源

<!-- Using classpath relative resources -->
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>

1.2mapper标签,通过url引入网络资源或者本地磁盘资源

<!-- Using url fully qualified paths -->
<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  <mapper url="file:///var/mappers/BlogMapper.xml"/>
  <mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>

1.3 第三种方式:mapper标签,通过class属性指定mapper接口名称,此时对应的映射文件必须与接口位于同一路径下,并且名称相同

如mapper接口采用注解的方式,则无需映射文件
windows系统下,映射文件不区分大小写,linux系统没有验证

<!-- Using mapper interface classes -->
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>

1.4第四种方式:package标签,通过name属性指定mapper接口所在的包名 ,此时对应的映射文件必须与接口位于同一路径下,并且名称相同

如mapper接口采用注解的方式,则无需映射文件
windows系统下,映射文件不区分大小写,linux系统没有验证

<!-- Register all interfaces in a package as mappers -->
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>

2. mapperLocation属性(位于SqlSessionFactoryBean中):mapperLocation属性,主要用于指定mapper.xml文件所处的位置。

2.1如果Mapper.xml与Mapper.class在同一个包下且同名,spring 中MapperScannerConfigurer 扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。
2.2如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。(如idea中 maven 默认不打包java文件夹下的xml文件,未在pom.xml中配置resource的情况下)
此时spring是通过识别mapper.xml中的
namespace的值来确定对应的Mapper.class的。

<!--创建sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--关联到连接池-->
    <property name="dataSource" ref="dataSource"/>
    <!--加载mybatis 全局配置文件-->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <!--加载mapper的xml映射文件 ,配置该属性后全局配置文件可以不需要再配置<mappers>-->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

3. MapperScannerConfigurer:

自动扫描 将Mapper接口生成代理注入到Spring
3.1要创建 MapperScannerConfigurer,可以在 Spring 的配置中添加如下代码:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

basePackage 属性是让你为映射器接口文件设置基本的包路径。 你可以使用分号或逗号 作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。

mybatis 整合spring之mapperLocations配置的问题

今天尝试spring整合mybatis时遇到这么一个问题,就是在配置sqlSessionFactory时是否要配置mapperLocations的问题。

<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="datasource"></property>
    <property name="typeAliasesPackage" value="com.fan.entity"/>
      <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。 *是个通配符,代表所有的文件,**代表所有目录下 -->   
    <property name="mapperLocations" value="classpath:com/fan/mapper/*.xml" /> 
   
    <!--也可以引入mybatis配置文件 <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> -->
</bean>
<!-- 通过扫描的模式,扫描目录在com.lanyuan.mapper目录下的mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.fan.mapper"></property>
</bean>

结论是:如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。

如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。

此时spring是通过识别mapper.xml中的 namespace的值来确定对应的Mapper.class的。

相关文章