Page cover

Spring + Mybatis 使用多个数据库

需求场景

之前为 Spring 项目,连接单一数据源 Oracle 数据库。新开发的功能的表的所属用户,一个专门为该功能新建的 Oracle 用户。

实现方案

jdbc.properties

jdbc.mysource.driver=oracle.jdbc.driver.OracleDriver
jdbc.mysource.url=jdbc:oracle:thin:@url:port:service
jdbc.mysource.username=myusername
jdbc.mysource.password=mypassword

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

  <!-- 配置信息放在同目录下的config/connection.properties中,需要用file而不是classspath-->
	<context:property-placeholder location="file:./config/connection.properties"/>
	<!-- 上面的另外一种写法,使用其中一种 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations">
	        <list>
	            <value>classpath*:jdbc.properties</value>
	        </list>
	    </pproperty>
	</bean>
	 <!-- 配置数据库1-->
	<bean id="sourceDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${source.jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10"/>
		<property name="maxIdle" value="5"/>
	</bean>
	 <!-- 配置数据库2-->
	<bean id="antennaparaDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${antennapara.jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10"/>
		<property name="maxIdle" value="5"/>
	</bean>
	 <!-- 配置数据库3-->
	<bean id="tsysDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${tsys.jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10"/>
		<property name="maxIdle" value="5"/>
	</bean>
	 <!-- 动态数据库,**.**.**.DatabaseContextHolder 屏蔽了包名,为具体选择数据库的类 -->
	<bean id="dataSource" class="**.**.**.DatabaseContextHolder">
            <property name="defaultTargetDataSource" ref="sourceDataSource"/>
            <property name="targetDataSources">
                <map>
                    <entry key="sourceDataSource" value-ref="sourceDataSource"/>
                    <entry key="antennaparaDataSource" value-ref="antennaparaDataSource"/>
                    <entry key="tsysDataSource" value-ref="tsysDataSource"/>
                </map>
            </property>
        </bean>
		 <!-- 因为mybatis配置里面没有设置什么内容,所以就先屏蔽掉了 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
	<!-- 	<property name="configLocation" value="classpath:mybatis.xml"/> -->
	<!-- 	<property name="mapperLocations" value="classpath*:xx/xx/xx/xml/*Mapper.xml"/> -->
	<!-- 	<property name="typeHandlersPackage" value="xx.xx.handler"/> -->
	</bean>
	
	
</beans>

DatabaseContextHolder.java

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

/*
* @author: dolphinZhao
* @date:Aug 28, 2019
* @version:
* @description:
*/
public class DatabaseContextHolder extends AbstractRoutingDataSource{
	
    public static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

    public static void setSelectedDatabase(String databaseName){
        contextHolder.set(databaseName);
    }
    public static String getSelectedDatabase(){
        String databaseName= (String)contextHolder.get();
        return dbType;
    }
    public void clearContext(){
        contextHolder.remove();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return DatabaseContextHolder.getSelectedDatabase();
    }
}

使用

DatabaseContextHolder.setSelectedDatabase("sourceDataSource");
Mapper mapper = (Mapper) ac.getBean("mapper");
//调用mapper完成具体操作

参考链接

最后更新于