MyBatis Spring整合配置映射接口类与映射xml文件
本文转自http://blog.csdn.net/zht666/article/details/38706083
Spring整合MyBatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般会使用MapperScannerConfigurer,MapperScannerConfigurer会自动扫描basePackage指定的包,找到映射接口类和映射XML文件,并进行注入。配置如下:
- <!-- 数据源 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${${database.type}.jdbc.driverClassName}"/>
- <property name="jdbcUrl" value="${${database.type}.jdbc.url}"/>
- <property name="properties" ref="dataSourceProperties"/>
- <property name="autoCommitOnClose" value="true"/>
- <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
- <property name="initialPoolSize" value="${cpool.minPoolSize}"/>
- <property name="minPoolSize" value="${cpool.minPoolSize}"/>
- <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
- <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
- <property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
- <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
- </bean>
- <!--基于注解的事务管理-->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
- </bean>
- <!-- 扫描mybatis映射接口类 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.test.dsm"/>
- <property name="sqlSessionFactoryBeanName" value="lazySqlSessionFactory"/>
- </bean>
这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.test.dsm)
如果myBatis映射XML文件和映射接口文件不放在同一个包下怎么办呢?
如果在不同的包下,那就需要手动配置XML文件的路径了,只需要修改SqlSessionFactoryBean配置即可:
- <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
- <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。
- *是个通配符,代表所有的文件,**代表所有目录下 -->
- <property name="mapperLocations" value="classpath:com/test/mapper/mysql/**/*.xml" />
- </bean>
添加一个mapperLocations属性,指定加载xml文件的路径。
classpath:表示在classes目录中查找;
*:通配符表示所有文件;
**:表示所有目录下;
MyBatis官网说明如下:http://mybatis.github.io/spring/factorybean.html
Properties
SqlSessionFactory has a single required property, the JDBC DataSource . This can be any DataSource and should be configured just like any other Spring database connection.
One common property is configLocation which is used to specify the location of the MyBatis XML configuration file. One case where this is needed is if the base MyBatis configuration needs to be changed. Usually this will be <settings> or <typeAliases> sections.
Note that this config file does not need to be a complete MyBatis config. Specifically, any environments, data sources and MyBatis transaction managers will beignored . SqlSessionFactoryBean creates its own, custom MyBatis Environment with these values set as required.
Another reason to require a config file is if the MyBatis mapper XML files are not in the same classpath location as the mapper classes. With this configuration, there are two options. This first is to manually specify the classpath of the XML files using a <mappers> section in the MyBatis config file. A second option is to use themapperLocations property of the factory bean.
The mapperLocations property takes a list of resource locations. This property can be used to specify the location of MyBatis XML mapper files. The value can contain Ant-style patterns to load all files in a directory or to recursively search all paths from a base location. For example:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
</bean>
This will load all the MyBatis mapper XML files in the sample.config.mappers package and its sub-packages from the classpath.
MyBatis Spring整合配置映射接口类与映射xml文件的更多相关文章
- Mybatis+Spring整合后Mapper测试类编写
public class UserMapperTest { private ApplicationContext applicationContext; @Before public void ini ...
- 【插件】【idea】的Mybatis Plugin插件方便mapper接口方法和mapper XML文件之间来回切换
效果 安装 这是2019.2版本的,旧版的有点不一样
- springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题
解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...
- mybatis与spring整合配置
mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...
- Spring+Mybatis+Maven 整合配置
<?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="by ...
- java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件
APT的介绍: APT(Annotation Processing Tool)是一种注解处理工具,它对源代码文件进行检测,并找出源文件所包含的注解信息,然后针对注解信息进行额外的处理. 使用APT工具 ...
- Power Designer导出实体类和NHibernate xml文件
Power Designer导出实体类和NHibernate xml文件 今天研究了一下通过PowerDesigner生成实体类和NHibernate所需要的xml文件,方法是通过Power Desi ...
- mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口
搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里
- spring和mybatis的整合配置
参考自: http://www.cnblogs.com/wangmingshun/p/5674633.html 链接中的文章里一共有三种整合方式,太多了怕记混了. 我这里只保留第二种. spring中 ...
随机推荐
- dataTables使用整理(一)
初次使用dataTables,对一些用到的属性及遇到的问题做一个简要的记录 参考资料: http://blog.csdn.net/mickey_miki/article/details/8240477 ...
- codeforces 767C - Garland
题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\fra ...
- Spring boot Value注入 未整理 待完善
Springboot 热部署Springboot为开发者提供了一个名叫 spring-boot-devtools来使Springboot应用支持热部署,提供开发者的开发效率,无需手动重启Spring ...
- MySQL 并发测试中,线程数和数据库连接池的实验
我一直以来,对性能测试中,连接池的大小要如何配置,不是太清楚: 就我所知道的,就DB自带对连接数的限制,在sqlserver中用select @@connection 可以查到, 在代码中,可以配置D ...
- python3.5学习第二章(1)标准库,bytes
一.输出python库的路径: 1.sys标准库 import sysprint(sys.path) 结果: 'E:\\python练习\\python35学习\\Day2', 'E:\\python ...
- PHP如何安装redis扩展(Windows下)
PHP如何安装redis扩展(Windows下) 一.总结 一句话总结:下载扩展的dll,放入指定文件夹(php对应的扩展的目录php/ext),在配置文件php.ini中注册dll 尽量不要选择最新 ...
- JavaScript 第十章总结:first class functions
前言 这一章的内容是 advanced knowledge and use of functions. 讲了关于 function 的使用的一些特殊的方面. function expression 的 ...
- 新C# 操作Excel属性
C# 操作Excel属性 数字(Range.NumberFormatlocal 属性) 常规:Range.NumberFormatlocal = "G/通用格式" 数值:Range ...
- 我们一起学习WCF 第六篇文件传输
原文 http://www.cnblogs.com/LipeiNet/p/4653830.html 前言:文件的输出我们并不陌生,但是今天我写的是用wcf模式进行文件传输,我觉得一大好处就是能进 ...
- linux进程管理之优先级
进程优先级 nice ==================================================================================== Linu ...