ssm多数据源

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd ">
	
    <!-- 配置数据源  --> 
    <bean name="datasource" class="com.alibaba.druid.pool.DruidDataSource"  
        init-method="init" destroy-method="close">  
        <property name="url" value="${jdbc.urlmaster}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
    </bean>  
  
    <!-- 配置数据源1   -->
    <bean name="datasource1" class="com.alibaba.druid.pool.DruidDataSource"  
        init-method="init" destroy-method="close">  
        <property name="url" value="${jdbc.urlslave}" />  
        <property name="username" value="${jdbc.username}" />  
        <property name="password" value="${jdbc.password}" />  
    </bean>  
  
    <bean id="multipleDataSource" class="pers.aidenj.ostrich.common.DataSource.DynamicDataSource">  
        <property name="targetDataSources">  
            <map>  
                <entry key="datasource" value-ref="datasource"/>  
                <entry key="datasource1" value-ref="datasource1"/>  
            </map>  
        </property>  
        <property name="defaultTargetDataSource" ref="datasource"/>  <!-- 默认数据源 -->
    </bean>  
      
    <!-- 配置sqlSessionFactory 并读取mybatis的一些配置 -->  
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="multipleDataSource"></property>  
        <property name="mapperLocations" value="classpath:pers/aidenj/ostrich/mapping/*.xml" />
    </bean>  
  
   <!--  自动扫描 将Mapper接口生成代理注入到Spring -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="pers.aidenj.ostrich.mapper" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
    </bean>  
  
    <!-- 配置事物   -->
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="multipleDataSource"></property>  
    </bean>  
  
    <tx:annotation-driven transaction-manager = "transactionManager"/>  
  
    <!-- 事物的具体内容   -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">  
        <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>
	
</beans>
package pers.aidenj.ostrich.common.DataSource;

public class DynamicDataSourceHolder {
    /**
     * 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰
     */
    private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();

    public static String getDataSource() {
        return THREAD_DATA_SOURCE.get();
    }

    public static void setDataSource(String dataSource) {
        THREAD_DATA_SOURCE.set(dataSource);
    }

    public static void clearDataSource() {
        THREAD_DATA_SOURCE.remove();
    }

}
package pers.aidenj.ostrich.common.DataSource;

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

public class DynamicDataSource extends AbstractRoutingDataSource {  
  
    @Override  
    protected Object determineCurrentLookupKey() {  
        return DynamicDataSourceHolder.getDataSource();
    }  
  
}

DynamicDataSourceHolder.setDataSource("datasource");//数据源对应的名称

*determineCurrentLookupKey切换数据源

转载于:https://my.oschina.net/u/913564/blog/1553200

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值