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");//數據源對應的名稱java

*determineCurrentLookupKey切換數據源spring

相關文章
相關標籤/搜索