cnflat0 2012-08-01
本文非原创!
Spring下mybatis多数据源配置
介绍
本文描述了以mybatis作为持久层框架使用Spring的声明式事务时,如何配置多个数据源(即连接多个数据库),使用Spring的注解方式迚行依赖的注入和事务的管理。并且利用mybatis的spring插件自动扫描和装配Mapper接口。
先来看一个mybatis的单数据源配置<context:property-placeholderlocation="classpath:jdbc.properties"/>
<beanid="dataSource"class="org.logicalcobwebs.proxool.ProxoolDataSource">
<propertyname="alias"value="${alias}"/>
<propertyname="driver"value="${driver}"/>
<propertyname="driverUrl"value="${driverUrl}"/>
<propertyname="user"value="${user}"/>
<propertyname="password"value="${password}"/>
<propertyname="houseKeepingTestSql"value="${house-keeping-test-sql}"/>
<propertyname="maximumConnectionCount"value="${maximum-connection-count}"/>
<propertyname="minimumConnectionCount"value="${minimum-connection-count}"/>
<propertyname="prototypeCount"value="${prototype-count}"/>
<propertyname="simultaneousBuildThrottle"value="${simultaneous-build-throttle}"/>
<propertyname="trace"value="${trace}"/>
</bean>
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis-config.xml"></property>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
<!--自动扫描mapper接口-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="rhinoceros.persistence.dao"/>
</bean>
说明
1.数据库连接池使用Proxool,首先配置的就是Proxool的数据源
2.接下来配置Spring的声明式事务管理,这里我们使用全注解+自动装配的方式迚行Bena和事务声明。
3.mybatis配置为自动扫描Maper接口,只要指定Mapper接口所在的包,需要注意的是,Mapper接口对应的映射文件(.xml)也要在这个包里,且名字和接口的名字一致。
使用多个数据源时,mybatis的文档表示丌能使用自动扫描和自动装配,但实际使用时发现还是可以的。看一个配置文件先:
<context:property-placeholderlocation="classpath:jdbc.properties"/>
<!--proxool连接池-->
<beanid="dataSource"class="org.logicalcobwebs.proxool.ProxoolDataSource">
<propertyname="alias"value="${alias}"/>
<propertyname="driver"value="${driver}"/>
<propertyname="driverUrl"value="${driverUrl}"/>
<propertyname="user"value="${user}"/>
<propertyname="password"value="${password}"/>
<propertyname="houseKeepingTestSql"value="${house-keeping-test-sql}"/>
<propertyname="maximumConnectionCount"value="${maximum-connection-count}"/>
<propertyname="minimumConnectionCount"value="${minimum-connection-count}"/>
<propertyname="prototypeCount"value="${prototype-count}"/>
<propertyname="simultaneousBuildThrottle"value="${simultaneous-build-throttle}"/>
<propertyname="trace"value="${trace}"/>
</bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis-config.xml"/>
</bean>
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--注解式事务管理-->
<tx:annotation-driventransaction-manager="transactionManager"/>
<!--自动扫描mybatismapper接口-->
<beanname="mapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="rhino.persistence.mapper"/>
<propertyname="sqlSessionFactory"ref="sqlSessionFactory"/>
</bean>
<!--配置新的的数据源-->
<beanid="dataSource_1"class="org.logicalcobwebs.proxool.ProxoolDataSource">
<propertyname="alias"value="${db1.alias}"/>
<propertyname="driver"value="${driver}"/>
<propertyname="driverUrl"value="${db1.driverUrl}"/>
<propertyname="user"value="${db1.user}"/>
<propertyname="password"value="${db1.password}"/>
<propertyname="houseKeepingTestSql"value="${house-keeping-test-sql}"/>
<propertyname="maximumConnectionCount"value="${maximum-connection-count}"/>
<propertyname="minimumConnectionCount"value="${minimum-connection-count}"/>
<propertyname="prototypeCount"value="${prototype-count}"/>
<propertyname="simultaneousBuildThrottle"value="${simultaneous-build-throttle}"/>
<propertyname="trace"value="${trace}"/>
</bean>
<beanid="sqlSessionFactory_1"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource_1"/>
<propertyname="configLocation"value="classpath:mybatis-config-db1.xml"/>
</bean>
<beanid="transactionManager_1"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource_1"/>
</bean>
<tx:annotation-driventransaction-manager="transactionManager_1"/>
<!--自动扫描mybatismapper接口-->
<beanname="mapperScannerConfigurer_1"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value=rhino.persistence.db1"/>
<propertyname="sqlSessionFactory"ref="sqlSessionFactory_1"/>
</bean>
<!--springbeans组件扫描-->
<context:component-scanbase-package="rhino.domain"/>
说明
1.需要配置新的数据源、事务管理器、sqlSession工厂
2.mybatis配置文件也要独立出来,即2个数据源应该有2个mybatis配置文件,互相之间是独立的,注意配置sqlSessionFactory_1时注入的mybatis-config-db1.xml
3.Mapper接口要放在一个新的包里
4.2个以上的数据源同样配置