JavaWEB项目配置动态数据源

说明

项目中如果需要连接多个数据库,则需要配置动态数据源,对于使用Spring+MyBatis框架的项目来说配置动态数据源一般需要在SqlMapConfig.xml中配置
接下来是配置步骤:

步骤

  1. 在配置JDBC连接的文件中配置动态数据源
<?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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:p="http://www.springframework.org/schema/p"  xmlns:util="http://www.springframework.org/schema/util" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache"
     xsi:schemaLocation="
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache  
        http://www.springframework.org/schema/cache/spring-cache.xsd 
        http://www.springframework.org/schema/jdbc  
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/util  
        http://www.springframework.org/schema/util/spring-util.xsd"
        >
			<context:annotation-config/>
			    <tx:annotation-driven transaction-manager="transactionManager"/>
			    <!-- 引入配置文件 -->
			    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			        <property name="locations">
			            <list>
			                <value>classpath*:application.properties</value>
			            </list>
			        </property>
			    </bean>

		<!--数据源一-->		    
				<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
		     <property name="driverClassName"  value="${jdbc1.driver}"/>  
		    <!-- 基本属性 url、user、password -->
		        <property name="url" value="${jdbc1.url}"/>
		        <property name="username" value="${jdbc1.username}"/>
		        <property name="password" value="${jdbc1.password}"/> 
		  </bean>  
		  
		  <!--数据源二-->		    
				<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
		     <property name="driverClassName"  value="${jdbc1.driver}"/>  
		    <!-- 基本属性 url、user、password -->
		        <property name="url" value="${jdbc1.url}"/>
		        <property name="username" value="${jdbc1.username}"/>
		        <property name="password" value="${jdbc1.password}"/> 
		  </bean>  
			 <!--这里配置数据源管理工具类,自定义一个管理类用于设置以及获取当前数据源名称等操作,该类需要继承AbstractRoutingDataSource类并实现其中的抽象方法-->		     
				<bean id="dataSource" class="自定义DbcontrxtHolder类的全线名称(包名.类名)">
				<!-- 设置默认数据源 -->
			        <property name="defaultTargetDataSource" ref="dataSource1"/>
			        <property name="targetDataSources">
			            <map>
			            <!-- 配置数据源列表  key为切换数据源时所用的名称,value-ref为配置datasource的bean的id值 -->
			                <entry key="dataSource1" value-ref="dataSource1"/>
			                <entry key="dataSource2" value-ref="dataSource2"/>
			            </map>
			        </property>
			    </bean>	
			    <!--后面配置事务等其他项,这里不再列出--></beans>     
  1. 新建数据源管理工具DbcontrxtHolder类

package com.framework;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DbcontextHolder extends AbstractRoutingDataSource{
	
	public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    //添加动态数据源时指定名称,用于切换数据源时获取连接对象    其变量值为 targetDataSources键值 
	public static final String DATASOURCE1 = "dataSource1";
	public static final String DATASOURCE2 = "dataSource2";
	
    /**
     * 设置当前数据源
     * @param dbType
     */
    public static void setDbType(String dbType){
        contextHolder.set(dbType);
    }
    /**
     * 获得当前数据源
     * @return
     */
    public static String getDbType(){
        String dbType = (String)contextHolder.get();
        return dbType;
    }
    /**
     *清除上下文
     *
     */
    public static void clearContext(){
        contextHolder.remove();
    }
    
    @Override
    protected Object determineCurrentLookupKey() {
        return DbcontextHolder.getDbType();
    }
    
}

  1. 在业务中切换数据源
public void test(){
...业务代码

DbcontextHolder.setDbType(DbcontextHolder.DATASOURCE2);//切换数据源2
     //保存信息当前数据源
     if(!saveSendTaskSimple(sendTask)){
			return false;
		}
//清理上下文信息,恢复到默认数据源		
DbcontextHolder.clearContext();

...业务代码

}

配置并切换数据源的操作基本完成

发布了27 篇原创文章 · 获赞 27 · 访问量 4279

猜你喜欢

转载自blog.csdn.net/qq_41788977/article/details/102686465
今日推荐