zoukankan      html  css  js  c++  java
  • 采用多个数据源是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="classpath:jdbc.properties"/>
        </bean>
        <bean id="sqlServerDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.sqlserver.driver}"/>
            <property name="url" value="${jdbc.sqlserver.url}"/>
            <property name="username" value="${jdbc.sqlserver.username}"/>
            <property name="password" value="${jdbc.sqlserver.password}"/>
            <property name="initialSize" value="${jdbc.initialSize}"/>
            <property name="minIdle" value="${jdbc.minIdle}"/>
            <property name="maxIdle" value="${jdbc.maxIdle}"/>
            <property name="maxActive" value="${jdbc.maxActive}"/>
            <property name="maxWait" value="${jdbc.maxWait}"/>
            <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
            <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
            <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
            <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
            <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
            <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
        </bean>
        <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.mysql.driver}"/>
            <property name="url" value="${jdbc.mysql.url}"/>
            <property name="username" value="${jdbc.mysql.username}"/>
            <property name="password" value="${jdbc.mysql.password}"/>
            <property name="initialSize" value="${jdbc.initialSize}"/>
            <property name="minIdle" value="${jdbc.minIdle}"/>
            <property name="maxIdle" value="${jdbc.maxIdle}"/>
            <property name="maxActive" value="${jdbc.maxActive}"/>
            <property name="maxWait" value="${jdbc.maxWait}"/>
            <property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
            <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
            <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
            <property name="testWhileIdle" value="${jdbc.testWhileIdle}"/>
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
            <property name="numTestsPerEvictionRun" value="${jdbc.numTestsPerEvictionRun}"/>
            <property name="minEvictableIdleTimeMillis" value="${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="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();
        }
    }
  • 相关阅读:
    Server2012R2 ADFS3.0 The same client browser session has made '6' requests in the last '13'seconds
    pig的grunt中shell命令不稳定,能不用尽量不用
    pig脚本的参数传入,多个参数传入
    pig的cogroup详解
    pig的limit无效(返回所有记录)sample有效
    Dynamics CRM2013 任务列表添加自定义按钮
    简述浏览器渲染机制
    如何区分浏览器类型
    使用mock.js生成前端测试数据
    理解Ajax
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/6604084.html
Copyright © 2011-2022 走看看