zoukankan      html  css  js  c++  java
  • mybatis配置多数据源(利用spring的AbstractRoutingDataSource)

    主要是利用了spring的AbstractRoutingDataSource。

    直接上配置了:

    1. spring-mybatis.xml
        <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            <property name="url" value="${db_url}" />
            <property name="username" value="${db_username}" />
            <property name="password" value="${db_password}" />
            <property name="initialSize" value="1" />
            <property name="maxActive" value="20" />
            <property name="minIdle" value="1" />
            <property name="maxWait" value="60000" />
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <property name="minEvictableIdleTimeMillis" value="25200000" />
            <property name="removeAbandoned" value="true" />
            <property name="removeAbandonedTimeout" value="1800" />
            <property name="logAbandoned" value="true" />
            <property name="filters" value="mergeStat" />
        </bean>
    
        <bean name="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            <property name="url" value="${financial_model_url}" />
            <property name="username" value="${financial_model_username}" />
            <property name="password" value="${financial_model_password}" />
            <property name="initialSize" value="10" />
            <property name="maxActive" value="20" />
            <property name="minIdle" value="20" />
            <property name="maxWait" value="60000" />
            <property name="poolPreparedStatements" value="false" />
            <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <property name="minEvictableIdleTimeMillis" value="25200000" />
            <property name="removeAbandoned" value="true" />
            <property name="removeAbandonedTimeout" value="1800" />
            <property name="logAbandoned" value="true" />
            <property name="filters" value="mergeStat" />
        </bean>
    
        <bean name="dataSource3" class="com.alibaba.druid.pool.DruidDataSource"
              init-method="init" destroy-method="close">
            <property name="url" value="${oracle.jdbc.url}" />
            <property name="username" value="${oracle.jdbc.username}" />
            <property name="password" value="${oracle.jdbc.password}" />
            <property name="initialSize" value="1" />
            <property name="maxActive" value="20" />
            <property name="minIdle" value="1" />
            <property name="maxWait" value="60000" />
            <property name="poolPreparedStatements" value="true" />
            <property name="maxPoolPreparedStatementPerConnectionSize" value="33" />
            <property name="validationQuery" value="${orcale.validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <property name="minEvictableIdleTimeMillis" value="25200000" />
            <property name="removeAbandoned" value="true" />
            <property name="removeAbandonedTimeout" value="1800" />
            <property name="logAbandoned" value="true" />
            <property name="filters" value="mergeStat" />
        </bean>
    
        <bean id="multipleDataSource" class="com.utils.MultipleDataSource">
            <property name="targetDataSources">
                <map>
                    <entry key="dataSource" value-ref="dataSource"/>
                    <entry key="dataSource2" value-ref="dataSource2"/>
                    <entry key="dataSource3" value-ref="dataSource3"/>
                </map>
            </property>
            <property name="defaultTargetDataSource" ref="dataSource2"></property>
        </bean>
    MultipleDataSource.java

    public class MultipleDataSource extends AbstractRoutingDataSource {
    
        @Override
        protected Object determineCurrentLookupKey() {
            // TODO Auto-generated method stub
    
            return DataSourceTypeManager.get();
        }
    
    }
    DataSourceTypeManager.java(为了避免多个线程间造成并发问题,用了线程本地变量)

    public class DataSourceTypeManager {
    
        //
        public static final String DS_BIO = "dataSource";
        //
        public static final String DS_MODEL = "dataSource2";
        //
        public static final String DS_ORACLE_RD = "dataSource3";
    
    
        private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<String>(){
           /* @Override
            protected String initialValue(){
                return DS_SUNGBIO;
            }*/
        };
    
        public static void setDataSourceKey(String dataSource) {
            dataSourceKey.set(dataSource);
        }
        public static String get(){
            return dataSourceKey.get();
        }
        public static void set(String dataSourceType){
            dataSourceKey.set(dataSourceType);
        }
    
        public static void reset(){
            dataSourceKey.set(DS_BIO);
        }
    
        public static void cleanDataSource(){
            dataSourceKey.remove();
        }
    }

    使用方法:

    使用前,手动写:

    DataSourceTypeManager.setDataSourceKey(DataSourceTypeManager.DS_MODEL);

    嫌麻烦的话,也可以像如下参考文章,配置注解,然后增加aop:

    http://www.cnblogs.com/davidwang456/p/4318303.html

  • 相关阅读:
    net core 使用 rabbitmq
    asp.net core WebApi 返回 HttpResponseMessage
    asp.net core 2.1 WebApi 快速入门
    JQuery EasyUI combobox动态添加option
    php截取字符去掉最后一个字符
    JQuery EasyUI Combobox的onChange事件
    对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成
    Access2007 操作或事件已被禁用模式阻止解决办法
    Easyui 中 Tabsr的常用方法
    Win 7 IE11不能下载文件,右键另存为也不行
  • 原文地址:https://www.cnblogs.com/grey-wolf/p/6962869.html
Copyright © 2011-2022 走看看