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

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

  2. <beansxmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. xmlns:context="http://www.springframework.org/schema/context"

  7. xsi:schemaLocation="  

  8.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

  9.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  

  10.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  11.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  

  12.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"  

  13. default-autowire="byName"default-lazy-init="false">

  14.   <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。  

  15.   连接池配置如下-->

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

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

  18. <propertyname="url"

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

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

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

  22. </bean>

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

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

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

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

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

  28. </bean>

  29. <!--MapperScannerConfigurer配置-->

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

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

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

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

  34. </bean>

  35. </beans>

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

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

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

  2. import com.abc.domain.Teacher;  

  3. import org.springframework.stereotype.Component;  

  4. @Component("myTeacherMapper")  

  5. publicinterface TeacherMapper {  

  6. public Teacher getById(int id);  

  7. }  

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

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

      运行结果如下:

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值