2012-10-27


 


Spring下 mybatis 多数据源配置 


介绍 


本文描述了以 mybatis 作为持久层框架使用 Spring的声明式事务时,如何配置多个数据源(即连接多个数据库),使用 Spring 的注解方式迚行依赖的注入和事务的管理。并且利用 mybatis 的spring 插件自动扫描和装配 Mapper 接口。 


先来看一个mybatis 的单数据源配置


 


01


<context:property-placeholder location="classpath:jdbc.properties" />


02


 


03


<bean id="dataSource"class="org.logicalcobwebs.proxool.ProxoolDataSource" >


04


<property name= "alias" value="${alias}" />


05


<property name= "driver" value="${driver}" />


06


<property name= "driverUrl" value="${driverUrl}"      />


07


<property name= "user" value="${user}" />


08


<property name= "password" value="${password}" /> 


09


<property name= "houseKeepingTestSql" value="${house-keeping-test-sql}" />


10


<property name= "maximumConnectionCount" value="${maximum-connection-count}" />


11


<property name= "minimumConnectionCount" value="${minimum-connection-count}" />


12


<property name= "prototypeCount" value="${prototype-count}" />


13


<property name= "simultaneousBuildThrottle" value="${simultaneous-build-throttle}" />


14


<property name= "trace" value="${trace}" />


15


</bean>


16


<bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >


17


<property name= "dataSource" ref="dataSource" />


18


</bean>


19


<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"    >


20


<property name= "dataSource" ref="dataSource" />


21


<property name= "configLocation" value="classpath:mybatis-config.xml" ></property> 


22


</bean>


23


<tx:annotation-driven transaction-manager="transactionManager" />


24


<!-- 自动扫描mapper 接口 -->


25


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >


26


<property name="basePackage" value="rhinoceros.persistence.dao" />


27


</bean>


说明 


1. 数据库连接池使用Proxool,首先配置的就是Proxool的数据源 


2. 接下来配置Spring的声明式事务管理,这里我们使用全注解+自动装配的方式迚行Bena和事务声明。 


3. mybatis配置为自动扫描Maper接口,只要指定Mapper接口所在的包,需要注意的是,Mapper接口对应的映射文件(.xml)也要在这个包里,且名字和接口的名字一致。 


使用多个数据源时,mybatis的文档表示丌能使用自动扫描和自动装配,但实际使用时发现还是可以的。看一个配置文件先: 


 


01


<context:property-placeholder location="classpath:jdbc.properties" />


02


<!-- proxool连接池 -->


03


<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">


04


<property name="alias" value="${alias}" />


05


<property name="driver" value="${driver}" />


06


<property name="driverUrl" value="${driverUrl}" />


07


<property name="user" value="${user}" />


08


<property name="password" value="${password}" />


09


<property name="houseKeepingTestSql" value="${house-keeping-test-sql}" />


10


<property name="maximumConnectionCount" value="${maximum-connection-count}" />


11


<property name="minimumConnectionCount" value="${minimum-connection-count}" />


12


<property name="prototypeCount" value="${prototype-count}" />


13


<property name="simultaneousBuildThrottle" value="${simultaneous-build-throttle}" />


14


<property name="trace" value="${trace}" />


15


</bean>


16


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">


17


<property name="dataSource" ref="dataSource" />


18


<property name="configLocation" value="classpath:mybatis-config.xml"/>


19


</bean>


20


<bean id="transactionManager"


21


class="org.springframework.jdbc.datasource.DataSourceTransactionManager">


22


<property name="dataSource" ref="dataSource" />


23


</bean>


24


<!-- 注解式事务管理 -->


25


<tx:annotation-driven transaction-manager= "transactionManager" />


26


<!-- 自动扫描 mybatis mapper 接口 -->


27


<bean name="mapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer" >


28


<property name= "basePackage" value="rhino.persistence.mapper" />


29


<property name= "sqlSessionFactory" ref="sqlSessionFactory" />


30


</bean>


31


<!-- 配置新的的数据源 -->


32


<bean id="dataSource_1" class="org.logicalcobwebs.proxool.ProxoolDataSource" >


33


<property name= "alias" value="${db1.alias}" />


34


<property name= "driver" value="${driver}" />


35


<property name= "driverUrl" value="${db1.driverUrl}" />


36


<property name= "user" value="${db1.user}" />


37


<property name= "password" value="${db1.password}" />


38


<property name= "houseKeepingTestSql" value="${house-keeping-test-sql}" />


39


<property name= "maximumConnectionCount" value="${maximum-connection-count}" />


40


<property name= "minimumConnectionCount" value="${minimum-connection-count}" />


41


<property name= "prototypeCount" value="${prototype-count}" />


42


<property name= "simultaneousBuildThrottle" value="${simultaneous-build-throttle}" /> 


43


<property name= "trace" value="${trace}" />


44


</bean>


45


<bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean" >


46


<property name= "dataSource" ref="dataSource_1" />


47


<property name= "configLocation" value="classpath:mybatis-config-db1.xml" />


48


</bean>


49


<bean id="transactionManager_1"


50


class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >


51


<property name= "dataSource" ref="dataSource_1" />


52


</bean>


53


<tx:annotation-driven transaction-manager= "transactionManager_1" />


54


<!-- 自动扫描 mybatis mapper 接口 -->


55


<bean name="mapperScannerConfigurer_1"class="org.mybatis.spring.mapper.MapperScannerConfigurer" >


56


<property name= "basePackage" value=rhino.persistence.db1" />


57


<property name= "sqlSessionFactory" ref="sqlSessionFactory_1" />


58


</bean>


59


<!-- spring beans 组件扫描 -->


60


<context:component-scan base-package="rhino.domain" />


 


说明 


   


1. 需要配置新的数据源、事务管理器、 sqlSession 工厂 


2. mybatis配置文件也要独立出来,即 2 个数据源应该有 2个 mybatis 配置文件,互相之间是独立的,注意配置 sqlSessionFactory_1 时注入的mybatis-config-db1.xml 


3. Mapper接口要放在一个新的包里 


4. 2 个以上的数据源同样配置