MyBatis MapperScannerConfigurer配置――MyBatis学习笔记之八

MyBatis MapperScannerConfigurer配置——MyBatis学习笔记之八
2012-09-02 20:01:42

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://legend2011.blog.51cto.com/3018495/980150

      在上一篇博文的示例中,我们在beans.xml中配置了studentMapper和teacherMapper,供我们需要时使用。但如果需要用到的映射器较多的话,采用这种配置方式就显得很低效。为了解决这个问题,我们可以使用MapperScannerConfigurer,让它扫描特定的包,自动帮我们成批地创建映射器。这样一来,就能大大减少配置的工作量。如下所示(点击此处进入本示例源程序下载页面):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。
连接池配置如下-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/courseman"/>
<property name="username" value="courseman"/>
<property name="password" value="abc123"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource"/>
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="resources/configuration.xml"/>
</bean>
<!--MapperScannerConfigurer配置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage指定要扫描的包,在此包之下的映射器都会被
搜索到。可指定多个包,包与包之间用逗号或分号分隔-->
<property name="basePackage" value="com.abc.mapper"/>
</bean>
</beans>

      这里需要注意三点:

      第一,无需指定引用SqlSessionFactory,因为MapperScannerConfigurer在创建映射器时会通过自动装配的方式来引用。

      第二,创建的映射器的命名问题。从beans.xml文件中我们可以看出,我们没有办法给MapperScannerConfigurer创建的这些映射器指定id或name属性,它们对我们似乎是不可见的。这个问题的解决之道在于采用了Spring针对自动侦测到的组件的默认命名策略,亦即把类/接口名字的首字母小写,其他不变,作为映射器的名字。例如,映射器接口TeacherMapper被扫描后创建的映射器bean名为teacherMapper。因此,我们可以像以前一样使用这样的代码来得到TeacherMapper实例:

1
TeacherMapper mapper  =  (TeacherMapper)ctx.getBean("teacherMapper");

      第三,可以使用@Component注解给映射器指定名称(本示例的源程序即是采用这种方法)。这里以TeacherMapper为例,若想指定生成的映射器bean名称为“myTeacherMapper”,步骤如下:

      1、在TeacherMapper接口中增加如下声明:“import org.springframework.stereotype.Component;”;

      2、在接口声明前添加@Component("myTeacherMapper")注解,即指定生成的映射器名称为myTeacherMapper。

      源码(TeacherMapper.java)如下:

1
2
3
4
5
6
7
package com.abc.mapper;
import com.abc.domain.Teacher;
import org.springframework.stereotype.Component;
@Component("myTeacherMapper")
public interface TeacherMapper {
public Teacher getById(int id);
}

      相应地,在程序中访问此映射器的代码应改为:

1
TeacherMapper mapper = (TeacherMapper)ctx.getBean("myTeacherMapper");

      运行结果如下:

194913431.png

      还有一点顺便提及,若映射器接口(如TeacherMapper接口)与相应的映射配置文件(如TeacherMapper.xml)同名且在同一目录下,就无需在核心配置文件configuration.xml中使用mappers元素来指定映射配置文件了。读者可自行实验。

参考资料:

1、http://www.mybatis.org/spring/zh/mappers.html#MapperScannerConfigurer(中文)

2、http://www.mybatis.org/spring/mappers.html#MapperScannerConfigurer(英文)

      猛戳这里全面系统地学习MyBatis 3

      MyBatis技术交流群:188972810,或扫描二维码:

wKioL1SaztmBchKiAADsv4YAWBY259.jpg


【MyBatis学习笔记】系列之预备篇一:ant的下载与安装

【MyBatis学习笔记】系列之预备篇二:ant入门示例

【MyBatis学习笔记】系列之一:MyBatis入门示例

【MyBatis学习笔记】系列之二:MyBatis增删改示例

【MyBatis学习笔记】系列之三:MyBatis的association示例

【MyBatis学习笔记】系列之四:MyBatis association的两种形式

【MyBatis学习笔记】系列之五:MyBatis与Spring集成示例

【MyBatis学习笔记】系列之六:MyBatis与Spring集成示例续

【MyBatis学习笔记】系列之七:MyBatis一对多双向关联

【MyBatis学习笔记】系列之八:MyBatis MapperScannerConfigurer配置

【MyBatis学习笔记】系列之九:MyBatis collection的两种形式

【MyBatis学习笔记】系列之十:MyBatis日志之Log4j示例

【MyBatis学习笔记】系列之十一:MyBatis多参数传递之注解方式示例

【MyBatis学习笔记】系列之十二:MyBatis多参数传递之默认命名方式示例

【MyBatis学习笔记】系列之十三:MyBatis多参数传递之Map方式示例

【MyBatis学习笔记】系列之十四:MyBatis中的N+1问题

【MyBatis学习笔记】系列之十五:MyBatis多参数传递之混合方式

【MyBatis学习笔记】系列之十六:Spring声明式事务管理示例

【MyBatis学习笔记】系列之十七:MyBatis多对多保存示例

【MyBatis学习笔记】系列之十八:MyBatis多对多关联查询示例

【MyBatis学习笔记】系列之十九:如何在MyBatis-3.2.7中使用Log4j2 rc2

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(一)

MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO(二)

本文出自 “肖凡的专栏” 博客,请务必保留此出处http://legend2011.blog.51cto.com/3018495/980150

原文地址:https://www.cnblogs.com/handsome1013/p/5190013.html