Spring粘合iBatis时多种配置SqlMapConfig.xml路径的方法

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

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">    
        <!-- 1. 配置单个SqlMapConfig.xml, 使用configLocation属性-->  
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>    

        <!-- 2. 配置多个SqlMapConfig.xml, 使用configLocations属性-->  
        <!-- 不包含class目录下的SqlMapConfig.xml -->  
        <property name="configLocations">  
                <list>  
                        <value>classpath:com/xxx/a/A-SqlMapConfig.xml</value>  
                        <value>classpath:com/xxx/b/B-SqlMapConfig.xml</value>  
                </list>   
        </properties>  

        <!-- 3. 匹配多个SqlMapConfig.xml, 使用mappingLocation属性-->  
        <!-- 不包含class目录下的SqlMapConfig.xml -->  
        <property name="mappingLocation" value="classpath:com/xxx/*/*-SqlMapConfig.xml"/>  

        <!-- 其他配置,例如dataSource等等 -->   
        <property name="dataSource" ref="dataSource"/>    
</bean>    

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

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

<?xml version="1.0" encoding="UTF-8"?>    
<sqlMapConfig>    

     <!-- <sqlMapImport resource="" url=""/> -->    

     <sqlMapImport resource="com/xxx/a/A-SqlMapConfig.xml"/>    
     <sqlMapImport resource="com/xxx/b/B-SqlMapConfig.xml"/>   

</sqlMapConfig> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章