Spring配置多数据源切换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36476972/article/details/82108755

多数据源切换

db.properties


#MySQL
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?autoReconnect=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=admin

#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=1000
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000




#MySQL
# driverClassName  根据url自动识别  这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName
jdbc.driver1=com.mysql.jdbc.Driver
jdbc.url1=jdbc:mysql://localhost:3306/test1?allowMultiQueries=true&autoReconnect=true&characterEncoding=utf-8
jdbc.username1=root
jdbc.password1=admin


# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
initialSize1=0

# 最大连接池数量
maxActive1=1000

#定义最小空闲
minIdle1=1

# 获取连接时最大等待时间,单位毫秒。配置了maxWait之后, 
# 缺省启用公平锁,并发效率会有所下降, 
# 如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
maxWait1=60000

# druid 监控
# 属性类型是字符串,通过别名的方式配置扩展插件, 
# 常用的插件有: 
# 监控统计用的filter:stat  
# 日志用的filter:log4j 
# 防御sql注入的filter:wall
filters1=stat,log4j

# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis1=60000

# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis1=300000

# 建议配置为true,不影响性能,并且保证安全性。 
# 申请连接的时候检测,如果空闲时间大于 
# timeBetweenEvictionRunsMillis, 
# 执行validationQuery检测连接是否有效。
testWhileIdle1=true

# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
testOnBorrow1=false

# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testOnReturn1=false

# 是否缓存preparedStatement,也就是PSCache。
# PSCache对支持游标的数据库性能提升巨大,比如说oracle。 
# 在mysql5.5以下的版本中没有PSCache功能,建议关闭掉。
# 作者在5.5版本中使用PSCache,通过监控界面发现PSCache有缓存命中率记录, 
# 该应该是支持PSCache。
poolPreparedStatements1=false

# 要启用PSCache,必须配置大于0,当大于0时, 
# poolPreparedStatements自动触发修改为true。 
# 在Druid中,不会存在Oracle下PSCache占用内存过多的问题, 
# 可以把这个数值配置大一些,比如说100
maxOpenPreparedStatements1=-1

applicationContext.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:tx="http://www.springframework.org/schema/tx"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/websocket
        http://www.springframework.org/schema/websocket/spring-websocket.xsd">

    <!-- spring使用注解  配置扫描   com.ys下的扫描 但是在springmvc里边已经扫描一次了
     这时就要加上属性 use-default-filters="true"   这个属性就是使用默认的扫描 
     默认就扫描com.ys下所有 设置为false 在下边配置需要扫描的部分-->

     <!-- 现在的配置就只会扫描带@Service和@Repository注解的类 -->
    <context:component-scan base-package="com" use-default-filters="false">
    <!-- org.springframework.stereotype.Service就是注解@service 这个注解使用在service里 所以就是扫描service包  
    org.springframework.stereotype.Repository   Repository仓库-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.beans.factory.annotation.Autowired"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
    <!-- 读取properties文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>


    <!-- 配置连接池数据源   文档搜索BasicDataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- results in a setDriverClassName(String) call -->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>


    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"  destroy-method="close">
        <property name="url" value="${jdbc.url1}" />
        <property name="username" value="${jdbc.username1}" />
        <property name="password" value="${jdbc.password1}" />
        <property name="driverClassName" value="${jdbc.driver1}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize1}"/>
        <!-- 最小空闲 -->
        <property name="minIdle" value="${minIdle1}" />
        <!--  最大连接池数量 -->
        <property name="maxActive" value="${maxActive1}" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait1}"/>
        <!-- 监控统计用的filter:stat   日志用的filter:log4j  防御sql注入的filter:wall -->
        <property name="filters" value="${filters1}" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis1}" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis1}" />
        <!-- 建议配置为true,不影响性能,并且保证安全性。 申请连接的时候检测 -->
        <property name="testWhileIdle" value="${testWhileIdle1}"/>
        <!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 -->
        <property name="testOnBorrow" value="${testOnBorrow1}"/>
        <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
        <property name="testOnReturn" value="false" />
        <!-- 是否缓存preparedStatement,也就是PSCache  适用支持游标的数据库  如Oracle -->
        <property name="poolPreparedStatements" value="${poolPreparedStatements1}"/>
        <!-- 要启用PSCache,必须配置大于0,当大于0时 poolPreparedStatements自动触发修改为true。 -->
        <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements1}"/>
        <!-- 定义监控日志输出间隔 -->
        <property name="timeBetweenLogStatsMillis" value="60000"/>
        <!--<property name="statLogger" ref ="statLoggerb"/> -->
        <!-- 若需要mybatis的批量sql需配置     不配置则报错:nested exception is java.sql.SQLException: sql injection violation, multi-statement not allow-->
        <property name="proxyFilters" ref="wall-filter"/>
    </bean>
    <!-- 若需要mybatis的批量sql需配置 -->
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">  
        <property name="config" ref="wall-config" />  
    </bean>  

    <bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">  
        <property name="multiStatementAllow" value="true" />  
    </bean> 



    <!-- 多数据源配置 -->
    <bean id="multipleDataSource" class="com.ys.dbConfig.MultipleDataSource">
        <!-- 默认数据源 -->
        <property name="defaultTargetDataSource" ref="dataSource" />
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry key="dataSource" value-ref="dataSource"/>
                <entry key="dataSource1" value-ref="dataSource1"/>
            </map>
        </property>
    </bean>



    <!-- 配置 sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="multipleDataSource"/>
        <property name="configLocation" value="classpath:mybatis.cfg.xml"/>
        <!-- 加载Mybatis全局配置文件 -->
        <property name="mapperLocations" value="classpath:com/ys/mapper/*.xml"/>
    </bean>

    <!-- 扫描的dao包(映射文件)  本身应该在mybatis里 现在交给spring -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ys.dao"/>
    </bean>


<!-- 事务配置 -->   
    <!-- 配置事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="multipleDataSource"></property>
    </bean>
    <!-- 开启注解事务 引用transactionManager -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

创建MultipleDataSource.java

package com.ys.dbConfig;

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


public class MultipleDataSource extends AbstractRoutingDataSource{

    @Override
    protected Object determineCurrentLookupKey() {
        return MultipleDataSourceHandler.getRouteKey();
    }

}

创建MultipleDataSourceHandler.java

package com.ys.dbConfig;  
/** 
*@Title MultipleDataSourceHandler.java 
*@description:  多数据源Handler
*@time 创建时间:2018年8月25日 上午10:52:12 
**/
public class MultipleDataSourceHandler {

    private static ThreadLocal<String> routeKey = new ThreadLocal<String>();


    /**
     * @Title: getRouteKey 
     * @Description: 获取当前线程的数据源路由的key
     * @param @return
     * @return String
     * @date createTime:2018年8月27日上午10:34:52
     */
    public static String getRouteKey(){
        return routeKey.get();
    }


    /**
     * @Title: setRouteKey 
     * @Description: 绑定当前线程数据源路由的key 使用完成后必须调用removeRouteKey()方法删除
     * @param @param key
     * @return void
     * @date createTime:2018年8月27日上午10:35:03
     */
    public static void  setRouteKey(String key){
        routeKey.set(key);
    }


    /**
     * @Title: removeRouteKey 
     * @Description: 删除与当前线程绑定的数据源路由的key
     * @return void
     * @date createTime:2018年8月27日上午10:35:31
     */
    public static void removeRouteKey(){
        routeKey.remove();
    }

}

切换数据源
MultipleDataSourceHandler.setRouteKey(“dataSource1”);

猜你喜欢

转载自blog.csdn.net/qq_36476972/article/details/82108755
今日推荐