首页 > 代码库 > 采用多个数据源是Spring的配置

采用多个数据源是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:aop="http://www.springframework.org/schema/aop"
       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/aop
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="http://www.mamicode.com/classpath:jdbc.properties"/>
    </bean>
    <bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="http://www.mamicode.com/${jdbc.sqlserver.driver}"/>
        <property name="url" value="http://www.mamicode.com/${jdbc.sqlserver.url}"/>
        <property name="username" value="http://www.mamicode.com/${jdbc.sqlserver.username}"/>
        <property name="password" value="http://www.mamicode.com/${jdbc.sqlserver.password}"/>
        <property name="initialSize" value="http://www.mamicode.com/${jdbc.initialSize}"/>
        <property name="minIdle" value="http://www.mamicode.com/${jdbc.minIdle}"/>
        <property name="maxIdle" value="http://www.mamicode.com/${jdbc.maxIdle}"/>
        <property name="maxActive" value="http://www.mamicode.com/${jdbc.maxActive}"/>
        <property name="maxWait" value="http://www.mamicode.com/${jdbc.maxWait}"/>
        <property name="defaultAutoCommit" value="http://www.mamicode.com/${jdbc.defaultAutoCommit}"/>
        <property name="removeAbandoned" value="http://www.mamicode.com/${jdbc.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="http://www.mamicode.com/${jdbc.removeAbandonedTimeout}"/>
        <property name="testWhileIdle" value="http://www.mamicode.com/${jdbc.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/${jdbc.timeBetweenEvictionRunsMillis}"/>
        <property name="numTestsPerEvictionRun" value="http://www.mamicode.com/${jdbc.numTestsPerEvictionRun}"/>
        <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/${jdbc.minEvictableIdleTimeMillis}"/>
    </bean>
    <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="http://www.mamicode.com/${jdbc.mysql.driver}"/>
        <property name="url" value="http://www.mamicode.com/${jdbc.mysql.url}"/>
        <property name="username" value="http://www.mamicode.com/${jdbc.mysql.username}"/>
        <property name="password" value="http://www.mamicode.com/${jdbc.mysql.password}"/>
        <property name="initialSize" value="http://www.mamicode.com/${jdbc.initialSize}"/>
        <property name="minIdle" value="http://www.mamicode.com/${jdbc.minIdle}"/>
        <property name="maxIdle" value="http://www.mamicode.com/${jdbc.maxIdle}"/>
        <property name="maxActive" value="http://www.mamicode.com/${jdbc.maxActive}"/>
        <property name="maxWait" value="http://www.mamicode.com/${jdbc.maxWait}"/>
        <property name="defaultAutoCommit" value="http://www.mamicode.com/${jdbc.defaultAutoCommit}"/>
        <property name="removeAbandoned" value="http://www.mamicode.com/${jdbc.removeAbandoned}"/>
        <property name="removeAbandonedTimeout" value="http://www.mamicode.com/${jdbc.removeAbandonedTimeout}"/>
        <property name="testWhileIdle" value="http://www.mamicode.com/${jdbc.testWhileIdle}"/>
        <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/${jdbc.timeBetweenEvictionRunsMillis}"/>
        <property name="numTestsPerEvictionRun" value="http://www.mamicode.com/${jdbc.numTestsPerEvictionRun}"/>
        <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/${jdbc.minEvictableIdleTimeMillis}"/>
    </bean>
    <bean id="multipleDataSource" class="com.cnblogs.lzrabbit.MultipleDataSource">
        <property name="defaultTargetDataSource" ref="mySqlDataSource"/>
        <property name="targetDataSources">
            <map>
                <entry key="mySqlDataSource" value-ref="mySqlDataSource"/>
                <entry key="sqlServerDataSource" value-ref="sqlServerDataSource"/>
            </map>
        </property>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="multipleDataSource"/>
    </bean>

    <!-- mybatis.spring自动映射 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="http://www.mamicode.com/com.cnblogs.lzrabbit"/>
    </bean>

    <!-- 自动扫描,多个包以 逗号分隔 -->
    <context:component-scan base-package="com.cnblogs.lzrabbit"/>
    <aop:aspectj-autoproxy/>
</beans>

整合数据源用到的

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

/**
 * Created by rabbit on 14-5-25.
 */
public class MultipleDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();

    public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return dataSourceKey.get();
    }
}

 

采用多个数据源是Spring的配置