您的位置:首页 > 移动开发

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

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

<beansxmlns="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目录下。

连接池配置如下-->

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>

<propertyname="url"

value="jdbc:mysql://localhost/courseman"/>

<propertyname="username"value="courseman"/>

<propertyname="password"value="abc123"/>

</bean>

<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<!--dataSource属性指定要用到的连接池-->

<propertyname="dataSource"ref="dataSource"/>

<!--configLocation属性指定mybatis的核心配置文件-->

<propertyname="configLocation"value="resources/configuration.xml"/>

</bean>

<!--MapperScannerConfigurer配置-->

<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!--basePackage指定要扫描的包,在此包之下的映射器都会被

搜索到。可指定多个包,包与包之间用逗号或分号分隔-->

<propertyname="basePackage"value="com.abc.mapper"/>

</bean>

</beans>

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

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

第三,可以使用@Component注解给映射器指定名称(本示例的源程序即是采用这种方法)。这里以TeacherMapper为例,若想指定生成的映射器bean名称为“myTeacherMapper”,步骤如下:
1、在TeacherMapper接口中增加如下声明:“import org.springframework.stereotype.Component;”;
2、在接口声明前添加@Component("myTeacherMapper")注解,即指定生成的映射器名称为myTeacherMapper。
源码(TeacherMapper.java)如下:
package com.abc.mapper;

import com.abc.domain.Teacher;

import org.springframework.stereotype.Component;

@Component("myTeacherMapper")

publicinterface TeacherMapper {

public Teacher getById(int id);

}

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

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

运行结果如下:



还有一点顺便提及,若映射器接口(如TeacherMapper接口)与相应的映射配置文件(如TeacherMapper.xml)同名且在同一目录下,就无需在核心配置文件configuration.xml中使用mappers元素来指定映射配置文件了。读者可自行实验。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: