SpringMvc+MyBatis 多数据源配置

1.jdbc.properties 添加第二个数据源信息(type2,driver2,url2,username2,pawwword2)

jdbc.type2=oracle
jdbc.driver2=oracle.jdbc.driver.OracleDriver
jdbc.url2=jdbc:oracle:thin:@localhost:1521:oracle
jdbc.username2=test
jdbc.password2=test                    

2.修改spring-context.xml(src/main/resources/),有3处需要修改/添加
第一处,添加bean id="dataSource2"

<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
        <property name="driverClassName" value="${jdbc.driver2}" />
        
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url2}" />
        <property name="username" value="${jdbc.username2}" />
        <property name="password" value="${jdbc.password2}" />
        
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${jdbc.pool.init}" />
        <property name="minIdle" value="${jdbc.pool.minIdle}" /> 
        <property name="maxActive" value="${jdbc.pool.maxActive}" />
        
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />
        
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
        
        <property name="validationQuery" value="${jdbc.testSql}" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
        
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat" /> 
    </bean>

第二处(spring-context.xml):修改为sqlSessionFactory bean,将dataSource改为dynamicDataSource

<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
...
    </bean>
<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dynamicDataSource"/>
...
    </bean>

第三处 (spring-context.xml):修改为transactionManager bean
同第二处一样 将ref="dataSource" 改为ref="dynamicDataSource"
并添加 dynamicDataSource bean

  <bean id="dynamicDataSource" class="com.thinkgem.jeesite.common.db.DynamicDataSource">  
        <property name="defaultTargetDataSource" ref="dataSource"/>  
        <property name="targetDataSources">  
            <map>  
                <entry key="dataSource" value-ref="dataSource"/>  
                <entry key="dataSource2" value-ref="dataSource2"/>  
            </map>  
        </property>  
    </bean>  

。 2016/11/23 14:44:57

**3.添加DynamicDataSource.java **

package com.thinkgem.jeesite.common.db;
 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
 
/**
 * Mysql 多数据源切换
 *
 * @author yulong
 * @version V1.0
 * @Description:
 * @date 2018/08/18
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
       
  
    public static String getCurrentLookupKey() {  
        return (String) contextHolder.get();  
    }  
   
 
    public static void setCurrentLookupKey(String currentLookupKey) {  
        contextHolder.set(currentLookupKey);  
    }  
  
    @Override  
    protected Object determineCurrentLookupKey() {  
        return getCurrentLookupKey();  
    }  
}

4.在Controller中控制数据源

            DynamicDataSource.setCurrentLookupKey("dataSource2");  
            Dy list = dyService.get("1");
            System.out.println("******************************333"+list.getId());
            model.addAttribute("list",list);  
            DynamicDataSource.setCurrentLookupKey("dataSource");

**注: 要对切换的数据源dataSource2 中的表手动写映射和三层实体 **
 

猜你喜欢

转载自blog.csdn.net/liu_yulong/article/details/81807286