spring2 多数据库动态切换 案例

1. 创建class----SourceType.java
public enum SourceType {
suning,
english,
gehualily
}


2. 创建class ---DynamicDataSource.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  

public class DynamicDataSource extends AbstractRoutingDataSource{

protected Object determineCurrentLookupKey() {
return JdbcContextHolder.getJdbcType();
}

}


3. 创建class ----JdbcContextHolder.java

public class JdbcContextHolder {

private static final ThreadLocal<SourceType> contextHolder = new ThreadLocal<SourceType>();

public static void setJdbcType(SourceType jdbcType) {

contextHolder.set(jdbcType);

}

/*
* public static String getJdbcType() { return (String) contextHolder.get(); }
*/
//默认的数据源
public static SourceType getJdbcType() {
SourceType str = (SourceType) contextHolder.get();
if(str==null){
str=SourceType.english;
}
return str;
}

public static void clearJdbcType() {
contextHolder.remove();

}

}



4. spring.xml:

<?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:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!--以下为数据源,可根据需求添加多个-->

<bean id="SuNingDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="driverUrl" value="jdbc:mysql://localhost:3306/suning" />
<property name="user" value="root" />
<property name="password" value="123" />

</bean>

<bean id="EnglishataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="driverUrl" value="jdbc:mysql://localhost:3306/lilye" />
<property name="user" value="root" />
<property name="password" value="123" />
</bean>

<bean id="GHLilyDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="driverUrl" value="jdbc:mysql://192.168.1.1:3306/test" />
<property name="user" value="root" />
<property name="password" value="123" />
</bean>

<!-- DynamicDataSource为上面的类,sourceType为枚举类,key为枚举的值,对应不同的数据源-->
<bean id="dataSource" class="cn.com.fly.util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="cn.com.fly.util.SourceType">
<entry key="suning" value-ref="SuNingDataSource"/>
<entry key="english" value-ref="EnglishDataSource"/>
<entry key="gehualily" value-ref="GHLilyDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="LilyEnglishDataSource"/>
</bean>
<!-- end defaultTargetDataSource为默认数据库-->

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="cn.com.fly.domain" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>

<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


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


<!-- 自动扫描的包名 -->
<context:component-scan base-package="cn.com.abc" />

<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />

<!-- 视图解释类 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 定时任务 -->
<task:annotation-driven/>

<!--
拦截器 <mvc:interceptors> <bean class="cn.com.fly.MyInteceptor" />
</mvc:interceptors>
-->

<!-- 对静态资源文件的访问
<mvc:default-servlet-handler/> -->
</beans>


5.应用案例:

在使用方法之前,加上
JdbcContextHolder.setJdbcType(SourceType.english);

若不用枚举类,key值可以直接用字符串。

实体类和表都不用添加什么别的代码,经过测试,每个方法只可切换一次数据库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值