当前位置:首页 > 开发 > 数据库 > 正文

spring2 多数据库动态切换 案例

发表于: 2012-07-30   作者:colddemon   来源:转载   浏览:
摘要: 1. 创建class----SourceType.java public enum SourceType { suning, english, gehualily } 2. 创建class ---DynamicDataSource.java import org.springframework.jdbc.datasource.lookup.Abst
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值可以直接用字符串。

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

spring2 多数据库动态切换 案例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号