Spring配置iBatis多个SqlMapConfig.xml

来源:互联网 发布:彩虹秒赞源码7.8 编辑:程序博客网 时间:2024/05/21 23:33

Spring粘合iBatis的时候需要配置iBatis的SqlMapConfig.xml

 

对于项目多个模块而又想同时能管理起来,普遍的单个SqlMapConfig.xml就会显得臃肿

 

可喜的是Spring已经为大家想好这一切,提供能灵活的配置

 

configLocation        // 单个SqlMapConfig.xml

configLocations      // 多个SqlMapConfig.xml

mappingLocations  // 自动匹配SqlMapConfig.xml

 

假设现在有几个配置文件,分别存放在不同的目录,结构如下

classes

|----SqlMapConfig.xml

|----com.xxx

       |----aModule

       |       |----A-SqlMapConfig.xml

       |----BModule

               |----B-SqlMapConfig.xml

 

现在通过Spring配置以上几个SqlMapConfig.xml

 

 

Java代码 复制代码
  1. <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">     
  2.         <!-- 1. 配置单个SqlMapConfig.xml, 使用configLocation属性-->   
  3.         <property name="configLocation" value="classpath:SqlMapConfig.xml"/>     
  4.     
  5.         <!-- 2. 配置多个SqlMapConfig.xml, 使用configLocations属性-->   
  6.         <!-- 不包含class目录下的SqlMapConfig.xml -->   
  7.         <property name="configLocations">   
  8.                 <list>   
  9.                         <value>classpath:com/xxx/a/A-SqlMapConfig.xml</value>   
  10.                         <value>classpath:com/xxx/b/B-SqlMapConfig.xml</value>   
  11.                 </list>    
  12.         </properties>   
  13.   
  14.         <!-- 3. 匹配多个SqlMapConfig.xml, 使用mappingLocation属性-->   
  15.         <!-- 不包含class目录下的SqlMapConfig.xml -->   
  16.         <property name="mappingLocation" value="classpath:com/xxx/*/*-SqlMapConfig.xml"/>   
  17.   
  18.         <!-- 其他配置,例如dataSource等等 -->    
  19.         <property name="dataSource" ref="dataSource"/>     
  20. </bean>    

 

这样,Spring就解决了多个模块下不同模块之前独立配置sqlMapConfog.xml的问题了。这个Spring2.5.5以后才支持

 

 

iBatis也能解决这类问题,不过要是iBatis高版本才支持,因为我使用的是2.3的,这方面就没有验证了

 

Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>     
  2. <sqlMapConfig>     
  3.       
  4.      <!-- <sqlMapImport resource="" url=""/> -->     
  5.           
  6.      <sqlMapImport resource="com/xxx/a/A-SqlMapConfig.xml"/>     
  7.      <sqlMapImport resource="com/xxx/b/B-SqlMapConfig.xml"/>    
  8.           
  9. </sqlMapConfig>