• 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

  • 相关阅读:
    文件上传&ContentType请求格式
    Ajax的简单操作
    外键的变种,单表和多表的查询
    Sql 库和表的基本操作、基本数据类型
    协程、IO多路复用、
    线程/进程锁、池,进程的数据共享
    进程和线程相关
    用wampserver安装thinksns时点击index.php显示空白
    类中的初始化函数作用
    python中的from XX import YY与import YY
  • 原文地址:https://www.cnblogs.com/grey-wolf/p/6962869.html
Copyright © 2020-2023  润新知