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




posted @   三国梦回  阅读(3768)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示