多数据源(sql server 2008,二个数据库不ip,)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自动扫描 ,多个包以 逗号分隔-->
<context:component-scan base-package="com.yunzhijia.cloudflow" />

<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<!-- 数据库测试 -->
<bean id="dataSource_cs" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.cs.driver}" />
<property name="url" value="${dataSource.cs.url}" />
<property name="username" value="${dataSource.cs.username}" />
<property name="password" value="${dataSource.cs.password}" />
<property name="initialSize" value="${dataSource.cs.initialSize}"></property>
<property name="maxActive" value="${dataSource.cs.maxActive}"></property>
<property name="maxIdle" value="${dataSource.cs.maxIdle}"></property>
<property name="minIdle" value="${dataSource.cs.minIdle}"></property>
<property name="maxWait" value="${dataSource.cs.maxWait}"></property>
</bean>

<!-- 数据库福清 -->
<bean id="dataSource_fq" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.fq.driver}" />
<property name="url" value="${dataSource.fq.url}" />
<property name="username" value="${dataSource.fq.username}" />
<property name="password" value="${dataSource.fq.password}" />
<property name="initialSize" value="${dataSource.fq.initialSize}"></property>
<property name="maxActive" value="${dataSource.fq.maxActive}"></property>
<property name="maxIdle" value="${dataSource.fq.maxIdle}"></property>
<property name="minIdle" value="${dataSource.fq.minIdle}"></property>
<property name="maxWait" value="${dataSource.fq.maxWait}"></property>
</bean>

<!-- 数据库河南 -->
<bean id="dataSource_hn" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.hn.driver}" />
<property name="url" value="${dataSource.hn.url}" />
<property name="username" value="${dataSource.hn.username}" />
<property name="password" value="${dataSource.hn.password}" />
<property name="initialSize" value="${dataSource.hn.initialSize}"></property>
<property name="maxActive" value="${dataSource.hn.maxActive}"></property>
<property name="maxIdle" value="${dataSource.hn.maxIdle}"></property>
<property name="minIdle" value="${dataSource.hn.minIdle}"></property>
<property name="maxWait" value="${dataSource.hn.maxWait}"></property>
</bean>
<bean id="dataSource" class="com.yunzhijia.cloudflow.common.utils.ThreadLocalRountingDataSource">
<property name="defaultTargetDataSource" ref="dataSource_cs"/>
<property name="targetDataSources">
<map key-type="com.yunzhijia.cloudflow.common.utils.DataSources">
<entry key="DATASOURCE_CS" value-ref="dataSource_cs"/>
<entry key="DATASOURCE_FQ" value-ref="dataSource_fq"/>
<entry key="DATASOURCE_HN" value-ref="dataSource_hn"/>
</map>
</property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/yunzhijia/cloudflow/mapping/**/*.xml"></property>
<!-- 指向mybatis配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yunzhijia.cloudflow.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务控制的注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

========================================

package com.yunzhijia.cloudflow.common.utils;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 数据源切换
* 新建ThreadLocalRountingDataSource继承AbstractRoutingDataSource,实现切换
* @author gzh
*
*/
public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
return DataSourceTypeManager.get();
}

}

========================================

package com.yunzhijia.cloudflow.common.utils;

import org.apache.log4j.Logger;

/**
* 数据源切换
* @author 
*
*/
public class DataSourceTypeManager {
private static final Logger log = Logger.getLogger(DataSourceTypeManager.class);
private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
@Override
protected DataSources initialValue(){
log.info("==>初始化数据源:"+DataSources.DATASOURCE_CS);
return DataSources.DATASOURCE_CS;
}
};
/**
* 取DataSources
* @return DataSources
*/
public static DataSources get(){
log.info("==>取数据源:"+dataSourceTypes.get());
return dataSourceTypes.get();
}
/**
* 设置DataSources源
* @param dataSourceType
*/
public static void set(DataSources dataSourceType){
log.info("==>设置数据源:"+dataSourceType);
dataSourceTypes.set(dataSourceType);
}
/**
* 重置数据库源
*/
public static void reset(){
log.info("==>重置数据源:"+DataSources.DATASOURCE_CS);
dataSourceTypes.set(DataSources.DATASOURCE_CS);
}
}

================================

package com.yunzhijia.cloudflow.common.utils;
/**
* 配置数据库切换
* @author
* 注:注意此枚举值必须对应spring配置文件各数据源的别名
*/
public enum DataSources {
DATASOURCE_CS,DATASOURCE_FQ,DATASOURCE_HN
}

原文地址:https://www.cnblogs.com/gzhbk/p/9628022.html