MyBatis Spring整合配置映射接口类与映射xml文件

Spring整合MyBatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般会使用MapperScannerConfigurer,MapperScannerConfigurer会自动扫描basePackage指定的包,找到映射接口类和映射XML文件,并进行注入。配置如下:

 

[html]  view plain  copy
 
  1. <!-- 数据源 -->  
  2. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  3.     <property name="driverClass" value="${${database.type}.jdbc.driverClassName}"/>  
  4.     <property name="jdbcUrl" value="${${database.type}.jdbc.url}"/>  
  5.     <property name="properties" ref="dataSourceProperties"/>  
  6.     <property name="autoCommitOnClose" value="true"/>  
  7.     <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>  
  8.     <property name="initialPoolSize" value="${cpool.minPoolSize}"/>  
  9.     <property name="minPoolSize" value="${cpool.minPoolSize}"/>  
  10.     <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>  
  11.     <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>  
  12.     <property name="acquireIncrement" value="${cpool.acquireIncrement}"/>  
  13.     <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>  
  14. </bean>  
  15.   
  16. <!--基于注解的事务管理-->  
  17. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  18.     <property name="dataSource" ref="dataSource"/>  
  19. </bean>  
  20.   
  21. <tx:annotation-driven transaction-manager="transactionManager"/>  
  22.   
  23.   
  24. <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  25.     <property name="dataSource" ref="dataSource"/>  
  26.     <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>  
  27. </bean>  
  28.   
  29. <!-- 扫描mybatis映射接口类 -->  
  30. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  31.     <property name="basePackage" value="com.test.dsm"/>  
  32.     <property name="sqlSessionFactoryBeanName" value="lazySqlSessionFactory"/>  
  33. </bean>  

这个配置的前提条件是:映射接口类文件(.java)和映射XML文件(.xml)需要放在相同的包下(com.test.dsm)

 

 

如果myBatis映射XML文件和映射接口文件不放在同一个包下怎么办呢?

如果在不同的包下,那就需要手动配置XML文件的路径了,只需要修改SqlSessionFactoryBean配置即可:

 

[html]  view plain  copy
 
  1. <bean id="lazySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  2.     <property name="dataSource" ref="dataSource"/>  
  3.     <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>  
  4.     <!-- 当mybatis的xml文件和mapper接口不在相同包下时,需要用mapperLocations属性指定xml文件的路径。  
  5.          *是个通配符,代表所有的文件,**代表所有目录下 -->  
  6.     <property name="mapperLocations" value="classpath:com/test/mapper/mysql/**/*.xml" />  
  7. </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.

 

转载于:https://www.cnblogs.com/likeju/p/5348343.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值