多数据源配对

来源:互联网 发布:js触发select选择事件 编辑:程序博客网 时间:2024/05/16 17:44
数据库配置


dataSource配置



<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- 导入资源文件 -->

    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置 C3P0 数据源 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!--用户名、密码、数据库URL、数据库驱动等信息配置 -->
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>


        <!--初始化连接数量 -->

        <property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    
    <bean id="dataSourceForTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${jdbc.user2}"></property>
            <property name="password" value="${jdbc.password2}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl2}"></property>
            <property name="driverClass" value="${jdbc.driverClass2}"></property>
            <property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
            <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        </bean>
    
        <bean id="dynamicDataSource" class="com.imagingunion.exam.register.util.DynamicDataSource" >  

            <!-- 通过key-value的形式来关联数据源 -->  

             <property name="targetDataSources">  
                <map key-type="com.imagingunion.exam.register.util.CustomerType">  
                    <entry value-ref="dataSource" key="dataSource"></entry>  
                    <entry value-ref="dataSourceForTwo" key="dataSourceForTwo"></entry>  
                </map>  
            </property>  
            <property name="defaultTargetDataSource" ref="dataSource" />  
        </bean>    


    <!-- 定义mybatis SqlSessionFactory,如果有多个domain包,逗号分割 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dynamicDataSource" />
        <property name="mapperLocations" value="classpath:com/imagingunion/**/mapper/*.xml" />

    </bean>


    <!--扫瞄Mapper,如果有多个Mapper包,逗号分割 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.**.mapper" />
    </bean>
</beans>




package com.imagingunion.exam.register.util;


/**
 *
 * @author GanLidong
 *
 *         2016-5-11
 */
public class CustomerContextHolder {
    // public static final String DATA_SOURCE = "dataSource";
    // public static final String DATA_SOURCE_TWO = "dataSourceForTwo";

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

    public static void setCustomerType(CustomerType customerType) {
        contextHolder.set(customerType);
    }

    public static CustomerType getCustomerType() {
        return (CustomerType) contextHolder.get();
    }

    public static void clearCustomerType() {
        contextHolder.remove();
    }
}



package com.imagingunion.exam.register.util;

public enum CustomerType {
    dataSource, dataSourceForTwo
}



package com.imagingunion.exam.register.util;

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

/**
 *
 * @author GanLidong
 *
 *         2016-5-11
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        System.out.println(CustomerContextHolder.getCustomerType());
        return CustomerContextHolder.getCustomerType();
    }

}






CustomerContextHolder.setCustomerType(CustomerType.dataSourceForTwo);调用

0 0
原创粉丝点击