沒有相應的RPC接口,一個項目存在多個數據源,不想在代碼端本身編寫代碼切換數據源?html
你有可能須要下面的解決方案:java
<!-- 數據源1配置 --> <bean id="dataSourceFor1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${db1_url}" /> <property name="username" value="$db1_user}" /> <property name="password" value="${db1_passwd}" /> <property name="maxWait" value="${db1_maxWait}" /> <property name="maxActive" value="28" /> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="300000" /> <property name="testOnBorrow" value="false" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="filters" value="stat" /> </bean> <!-- 建立sqlSessionFactory1--> <bean id="sqlSessionFactoryFor1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:com/**/mapper1/*Mapper*.xml" /> <property name="dataSource" ref="dataSourceFor1" /> </bean> <!-- 配置掃描器,掃描指定路徑的mapper生成數據庫操做代理類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.my.***.test.***.mapper1" /> <property name="sqlSessionFactory" ref="sqlSessionFactoryFor1" /> </bean> <!-- 數據源2配置 --> <bean id="dataSourceFor2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${db2_url}" /> <property name="username" value="$db2_user}" /> <property name="password" value="${db2_passwd}" /> <property name="maxWait" value="${db2_maxWait}" /> <property name="maxActive" value="28" /> <property name="initialSize" value="2" /> <property name="minIdle" value="0" /> <property name="timeBetweenEvictionRunsMillis" value="300000" /> <property name="testOnBorrow" value="false" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="filters" value="stat" /> </bean> <!-- 建立sqlSessionFactory2--> <bean id="sqlSessionFactoryFor2" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath*:com/**/mapper2/*Mapper*.xml" /> <property name="dataSource" ref="dataSourceFor2" /> </bean> <!-- 此處注意使用不一樣的sqlSessionFactory --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="javax.annotation.Resource"></property> <property name="basePackage" value="com.my.***.test.***.mapper2" /> <property name="sqlSessionFactory" ref="sqlSessionFactoryFor2" /> </bean>
<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource1"></property> <qualifier value="datasource1Tx"/> </bean> <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource2"></property> <qualifier value="datasource2Tx"/> </bean>
註解方式spring
在spring-mybatis配置文件中打開註解配置sql
<!-- 開啓 @Transactional 註解--> <tx:annotation-driven/>
上面的配置已經定義了多個transactional manager,並設置qualifier屬性指定不一樣的值;而後在須要使用@Transactional註解的時候指定TransactionManager的qualifier屬性值或者直接使用bean名稱,示例以下:數據庫
public class TransactionalService { @Transactional("datasource1Tx") public void updateSomethingIntoDatasource1() { ... } @Transactional("datasource2Tx") public void saveSomethingIntoDatasource2() { ... } } /** 另外一種方式: 直接使用transactin manager 的bean名字來開啓事物。 @Transactional("transactionManager1") 若是是使用@Transactional(),至關於使用缺省的transaction mananger名字, 即:@Transactional("transactionManager") */
注意:mybatis
上述配置方式只能保證同一數據源的事物的完整性,不保證一個service方法裏有對多個數據源更新操做的事物的完整性。mvc
若是一個事物裏須要對多個數據源進行更新操做,須要使用到分佈式事物,能夠參考springmvc+mybatis+atomikos+JTA的實現方案,這裏不作詳細描述。oracle