MyBatis Spring整合配置映射接口类与映射xml文件
来源:互联网 发布:软件升级必然凯文凯利 编辑:程序博客网 时间:2024/04/30 03:00
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整合配置映射接口类与映射xml文件
- MyBatis Spring整合配置映射接口类与映射xml文件
- MyBatis Spring整合配置映射接口类与映射xml文件
- MyBatis Spring整合配置映射接口类与映射xml文件
- MyBatis Spring整合配置映射接口类与映射xml文件
- spring+mybatis整合,不需要配置映射文件,多路径配置
- mybatis逆向生成实体类,接口类,映射xml文件
- mybatis 生成映射文件generatorConfig.xml配置
- mapper.xml映射文件配置 mybatis
- Mybatis XML映射文件
- Mybatis--XML映射文件
- mybatis 分页查询 内含有测试类实体类映射文件与映射接口
- 使用spring整合mybatis时映射文件的导入方法
- maven下 spring+springmvc+mybatis整合,映射文件加载异常
- MyBatis的XML映射文件、映射器
- Spring、MyBatis的整合数据映射器类(UserMapper->iocContext.xml)配置文件详解
- 整合maven+mybatis+generator生成java自定义model实体类,dao接口和mapper映射文件
- Intel Galileo笔记(四)windows on galileo的配置
- Sort Colors Java
- C++ 继承之访问权限
- Cocos2d-x 瓦块地图小游戏 (五) 城墙 与 吃瓜
- 小心骗子,买放心机票
- MyBatis Spring整合配置映射接口类与映射xml文件
- 网站是否要修改标题
- Afinal 框架 Afinal DB的学习
- OGNL使用小结
- 普通IO口模拟实现I2C通信及应用解析
- 音视频对讲SDK让游戏嗨起来
- 【unity3d Editor开发】简单节点编辑器(二)
- 搁浅岁月,幸福就在身边
- 堆与堆排序