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>