建设个公司网站需要多少费用,商城网站都有什么功能模块,网站 底部,怎样做网站代理在上一篇博文的示例中#xff0c;我们在beans.xml中配置了studentMapper和teacherMapper#xff0c;供我们需要时使用。但如果需要用到的映射器较多的话#xff0c;采用这种配置方式就显得很低效。为了解决这个问题#xff0c;我们可以使用MapperScannerConfigurer#xf… 在上一篇博文的示例中我们在beans.xml中配置了studentMapper和teacherMapper供我们需要时使用。但如果需要用到的映射器较多的话采用这种配置方式就显得很低效。为了解决这个问题我们可以使用MapperScannerConfigurer让它扫描特定的包自动帮我们成批地创建映射器。这样一来就能大大减少配置的工作量。如下所示点击此处进入本示例源程序下载页面 ?xml version1.0 encodingutf8?
beans xmlnshttp://www.springframework.org/schema/beans
xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance
xmlns:aophttp://www.springframework.org/schema/aop
xmlns:txhttp://www.springframework.org/schema/tx
xmlns:contexthttp://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-autowirebyName default-lazy-initfalse
!--本示例采用DBCP连接池应预先把DBCP的jar包复制到工程的lib目录下。
连接池配置如下--
bean iddataSource classorg.apache.commons.dbcp.BasicDataSource
property namedriverClassName valuecom.mysql.jdbc.Driver/
property nameurl valuejdbc:mysql://localhost/courseman/
property nameusername valuecourseman/
property namepassword valueabc123/
/bean
bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean
!--dataSource属性指定要用到的连接池--
property namedataSource refdataSource/
!--configLocation属性指定mybatis的核心配置文件--
property nameconfigLocation valueresources/configuration.xml/
/bean
!--MapperScannerConfigurer配置--
bean classorg.mybatis.spring.mapper.MapperScannerConfigurer
!--basePackage指定要扫描的包在此包之下的映射器都会被
搜索到。可指定多个包包与包之间用逗号或分号分隔--
property namebasePackage valuecom.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、http://www.mybatis.org/spring/zh/mappers.html#MapperScannerConfigurer中文 2、http://www.mybatis.org/spring/mappers.html#MapperScannerConfigurer英文 猛戳这里全面系统地学习MyBatis 3 MyBatis技术交流群188972810或扫描二维码 【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中的N1问题 【MyBatis学习笔记】系列之十五MyBatis多参数传递之混合方式 【MyBatis学习笔记】系列之十六Spring声明式事务管理示例 【MyBatis学习笔记】系列之十七MyBatis多对多保存示例 【MyBatis学习笔记】系列之十八MyBatis多对多关联查询示例 【MyBatis学习笔记】系列之十九如何在MyBatis-3.2.7中使用Log4j2 rc2 MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO一 MyBatis中如何通过继承SqlSessionDaoSupport来编写DAO二