MyBatis MapperScannerConfigurer配置

我们在beans.xml中配置了studentMapper和teacherMapper,供我们需要时使用。但如果需要用到的映射器较多的话,采用这种配置方式就显得很低效。为了解决这个问题,我们可以使用MapperScannerConfigurer,让它扫描特定的包,自动帮我们成批地创建映射器。这样一来,就能大大减少配置的工作量。

<?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实例:


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")
public interface TeacherMapper {
public Teacher getById(int id);
}


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

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值