JavaWEB项目配置动态数据源

说明

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

步骤

  1. 在配置JDBC连接的文件中配置动态数据源

<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.propertiesvalue>
			            list>
			        property>
			    bean>

				    
				<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
		     <property name="driverClassName"  value="${jdbc1.driver}"/>  
		    
		        <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}"/>  
		    
		        <property name="url" value="${jdbc1.url}"/>
		        <property name="username" value="${jdbc1.username}"/>
		        <property name="password" value="${jdbc1.password}"/> 
		  bean>  
			 		     
				<bean id="dataSource" class="自定义DbcontrxtHolder类的全线名称(包名.类名)">
				
			        <property name="defaultTargetDataSource" ref="dataSource1"/>
			        <property name="targetDataSources">
			            <map>
			            
			                <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();

...业务代码

}

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

你可能感兴趣的:(MyBatis,Spring,JavaEE)