spring+mybatis 多数据源配置(手动切换)

不说废话之间上硬菜:

1.添加多数源的配置文件:duridconfig.properties
 

# source.jdbc.*
blsc.jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
blsc.jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orl
blsc.jdbc.username=
blsc.jdbc.password=

hxsc.jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
hxsc.jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orl
hxsc.jdbc.username=
hxsc.jdbc.password=

shop.jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
shop.jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orl
shop.jdbc.username=
shop.jdbc.password=

trustee.jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
trustee.jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521/orl
trustee.jdbc.username=
trustee.jdbc.password=

2.重写数据源接口类MultipleDataSource.java

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

/**
 * @ClassName: MultipleDataSource
 * @Description: 
 * 多数据源实现类
 * @author lin.xu
 * @date 2018年5月18日
 *
 */
public class MultipleDataSource extends AbstractRoutingDataSource {
	
	public static final String DEFUL_DATA_SOURCE = "datasourceShop";  
    public static final String TRUSTEE_DATA_SOURCE = "datasourceTrustee"; 
    public static final String HXSC_DATA_SOURCE = "datasourceHxsc"; 
    public static final String BLSC_DATA_SOURCE = "datasourceBlsc"; 
    
    //链接对应的资源标识Key
    private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
	
    /* (非 Javadoc)
	 * @return
	 * @see org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#determineCurrentLookupKey()
	 */ 
	@Override
	protected Object determineCurrentLookupKey() {
		return dataSourceKey.get();
	}
	
	/**
	 * @Title: setDataSourceKey
	 * @Description: 
	 * 指定对应数据源
	 * @param dataSource
	 * void 返回值
	 * @author lin.xu
	 * @throws
	 */
	public static void setDataSourceKey(String dataSource) {
        dataSourceKey.set(dataSource);
    }

	/**
	 * @Title: restDataSourceKey
	 * @Description: 
	 * 重置数据源
	 * void 返回值
	 * @author lin.xu
	 * @throws
	 */
	public static void restDataSourceKey(){
		 dataSourceKey.set(DEFUL_DATA_SOURCE);
	}
}

 

3..配置xml文件信息druid_dataSource.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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xsi:schemaLocation="  
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
     ">
    <!-- 引入属性文件 -->
    <!-- <context:property-placeholder location="classpath:config.properties" /> -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
	    <property name="locations"> <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->  
	        <array>  
	            <value>classpath:config.properties</value>
	            <value>classpath:duridconfig.properties</value>  
	        </array>  
	    </property>  
	</bean> 
	
	<!-- 配置数据源 开始 -->  
    <bean name="datasourceShop" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
        <property name="driverClassName" value="${shop.jdbc.driverClassName}" /> 
        <property name="url" value="${shop.jdbc.url}"/>  
        <property name="username" value="${shop.jdbc.username}" />  
        <property name="password" value="${shop.jdbc.password}" />  
    </bean>
    <bean name="datasourceTrustee" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
        <property name="driverClassName" value="${trustee.jdbc.driverClassName}" /> 
        <property name="url" value="${trustee.jdbc.url}"/>  
        <property name="username" value="${trustee.jdbc.username}" />  
        <property name="password" value="${trustee.jdbc.password}" />  
    </bean>
    <bean name="datasourceHxsc" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
        <property name="driverClassName" value="${hxsc.jdbc.driverClassName}" /> 
        <property name="url" value="${hxsc.jdbc.url}"/>  
        <property name="username" value="${hxsc.jdbc.username}" />  
        <property name="password" value="${hxsc.jdbc.password}" />  
    </bean> 
    <bean name="datasourceBlsc" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
        <property name="driverClassName" value="${blsc.jdbc.driverClassName}" />  
        <property name="url" value="${blsc.jdbc.url}"/>  
        <property name="username" value="${blsc.jdbc.username}" />  
        <property name="password" value="${blsc.jdbc.password}" />  
    </bean> 
    <!-- 配置数据源 结束 -->
  
  	<!-- 懒汉式多数据源配置 -->
    <bean id="multipleDataSource" class="com.xxx.senior.MultipleDataSource">  
        <property name="defaultTargetDataSource" ref="datasourceShop"/>  
        <property name="targetDataSources">  
            <map>  
                <entry key="datasourceShop" value-ref="datasourceShop"/> 
                <entry key="datasourceTrustee" value-ref="datasourceTrustee"/> 
                <entry key="datasourceHxsc" value-ref="datasourceHxsc"/> 
                <entry key="datasourceBlsc" value-ref="datasourceBlsc"/>   
            </map>  
        </property>  
    </bean>
    
      
    <!--配置sqlSessionFactory 并读取mybatis的一些配置 -->  
    <bean name="sqlSessionFactorySlave" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="multipleDataSource"></property>  
        <property name="configLocation" value="classpath:mybatis-config.xml"/>  
        <property name="typeAliasesPackage" 
        	value="com.lincomb.common.models,
				com.lincomb.manager.system.models,
				com.lincomb.manager.shop.models,
				com.lincomb.manager.send.models,
				com.lincomb.manager.point.models,
				com.lincomb.manager.pushwb.models,
				com.lincomb.manager.bannner.models,
				com.lincomb.manager.coupon.models,
				com.lincomb.manager.sign.models,
				com.lincomb.shop.models,
				com.lincomb.point.models,
				com.lincomb.loan.models,
				com.lincomb.funcmg.model" />
        <!-- <property name="mapperLocations" value="classpath*:/config/com/lincomb/funcmg/*Mapper.xml" /> -->  
    </bean>  
  
  
    <!-- 自动扫描 将Mapper接口生成代理注入到Spring -->  
    <bean id="mapperScannerConfigurerSlave" class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" 
        	value="com.lincomb.common.mapper,
				com.lincomb.manager.system.mapper,
				com.lincomb.manager.shop.mapper,
				com.lincomb.manager.send.mapper,
				com.lincomb.manager.point.mapper,
				com.lincomb.manager.pushwb.mapper,
				com.lincomb.manager.bannner.mapper,
				com.lincomb.manager.sign.mapper,
				com.lincomb.manager.coupon.mapper,
				com.lincomb.shop.mapper,
				com.lincomb.point.mapper,
				com.lincomb.manager.backmove.mapper,
				com.lincomb.manager.loan.mapper,
				com.lincomb.manager.seal.mapper,
				com.lincomb.funcmg.mapper" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySlave" />  
    </bean> 
	
	<!-- 配置事物 -->
	<bean id="transactionManagerSlave"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="multipleDataSource"></property>  
    </bean>
    
    <!-- 事物的处理交由注解进行处理 -->  
    <tx:annotation-driven transaction-manager = "transactionManagerSlave"/>
    
    <!-- 事物的具体内容   该种属于xml配置模式
    <tx:advice id="transactionAdviceSlave" transaction-manager="transactionManagerSlave">  
        <tx:attributes>  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="append*" propagation="REQUIRED" />  
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="modify*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="repair" propagation="REQUIRED" />  
            <tx:method name="delAndRepair" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="SUPPORTS" />  
            <tx:method name="find*" propagation="SUPPORTS" />  
            <tx:method name="load*" propagation="SUPPORTS" />  
            <tx:method name="search*" propagation="SUPPORTS" />  
            <tx:method name="datagrid*" propagation="SUPPORTS" />  
            <tx:method name="*" propagation="SUPPORTS" />  
        </tx:attributes>  
    </tx:advice>
           配置切点 
    <aop:config>
    	<aop:pointcut id="transactionPointcutSlave" expression="execution(* com.lincomb.funcmg.service.*.*(..))" />
    </aop:config>
	-->
</beans>

4.手动切换数据源动作代码:

MultipleDataSource.setDataSourceKey(MultipleDataSource.TRUSTEE_DATA_SOURCE);

转载于:https://my.oschina.net/xulin/blog/1816006

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值