废话少说 直接上代码
1.创建数据源切换的class类
package com.util;
public class DataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDBType(String dbType) {
contextHolder.set(dbType);
}
public static String getDBType() {
return ((String) contextHolder.get());
}
public static void clearDBType() {
contextHolder.remove();
}
}
package com.util;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// TODO Auto-generated method stub
return DataSourceContextHolder.getDBType();
}
}
2在spring配置文件中写上两个数据源 两个与spring与mybatis的整合和两个
下面是我的spring配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com"></context:component-scan>
<!-- 配置springmvc -->
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="view"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/JSP/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置sqlserver数据源ds -->
<bean id="ds"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://10.157.1.100:1433;database=PowerGateLAOS" />
<property name="username" value="XX" />
<property name="password" value="XX" />
</bean>
<!-- 配置oracle数据源 dd -->
<bean id="dd"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@10.157.1.100:1522:laos"></property>
<property name="username" value="amibokeo"></property>
<property name="password" value="ami2016"></property>
</bean>
<!-- 配置sqlserver数据源spring和mybatis的整合 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds" />
<property name="mapperLocations" value="classpath:com/mapper/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!-- 配置oracle数据源的spring和mybatis的整合 -->
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dd" />
<property name="mapperLocations" value="classpath:com/mapper1/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper1" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1" />
</bean>
<!-- 配置两个数据源之间的切换 -->
<bean id="dataSource" class="com.util.DynamicDataSource">//这边引用上面的DynamicDataSource类的class路径
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="ds" key="ds"></entry>
<entry value-ref="dd" key="dd"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dd"></property>//默认使用oracle数据源
</bean>
<!-- 配置两个数据源的事务 -->
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds" />
</bean>
<bean
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dd" />
</bean>
</beans>
3在程序代码中直接使用
DataSourceContextHolder.setDBType("dd");这个方法来调用你想操作的数据库
这样spring多数据源就配置j实现对多个数据库操作就完成了
1.创建数据源切换的class类 名称要和spring配置文件中配置数据源切换中bean的class相同