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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值