Spring MVC + mybatis to configure multiple data sources

Keywords: xml Mybatis Database JDBC

Business scenario:

In the actual project development, sometimes the data will be stored in multiple databases, that is, there are different data sources. So in the SSM framework, how to configure multiple databases?

Get ready:

Two Oracle data sources

DataSource1:172.83.242.145:1521/orcl user name: zsyw · Jr password:******

DataSource2:172.83.30.209:1521/orcl user name: ydpt password:*******

Implementation ideas:

Step 1: configure DataSource

 <!-- Test environment database 1 -->
	<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
       <property name="url" value="jdbc:oracle:thin:@172.83.242.145:1521:orcl" />
        <property name="username" value="ZSYW_JR" />
        <property name="password" value="ZSYW_JR" />
    </bean>
    <!-- Test environment database 2 -->
    <bean id="dataSource2"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
       <property name="url" value="jdbc:oracle:thin:@172.83.30.209:1521:orcl" />
        <property name="username" value="ydpt" />
        <property name="password" value="ydpt" />
    </bean>

Step 2: configure sqlSessionFactory

 <!-- Configure data sources,load configuration,that is dataSource -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--mybatis Profile for-->
        <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <!--scanning XXXmapper.xml Mapping file,Configure path for scan-->
        <!-- <property name="mapperLocations" value="classpath:com/javafeng/mapping/*.xml"></property>
        <property name="mapperLocations" value="classpath:com/inspur/NSRZCXXGR/mapping/*.xml"></property> -->
        <property name="mapperLocations">
  			<array>
   				<value>classpath:com/inspur/SSO/Mapper/*.xml</value>
   				<value>classpath:com/inspur/test/dao/*.xml</value>
   				<value>classpath:com/inspur/NSRZCXXGR/mapping/*.xml</value>
 		    </array>
	    </property>
    </bean>
    
     <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2"></property>
        <!--mybatis Profile for-->
        <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <!--scanning XXXmapper.xml Mapping file,Configure path for scan-->
        <!-- <property name="mapperLocations" value="classpath:com/javafeng/mapping/*.xml"></property>
        <property name="mapperLocations" value="classpath:com/inspur/NSRZCXXGR/mapping/*.xml"></property> -->
        <property name="mapperLocations">
  			<array>
   				<value>classpath:com/inspur/test1/dao/*.xml</value>
 		    </array>
	    </property>
    </bean>

Step 3: configure and scan the xml file of mybatis to set different sqlSessionFactory

 <!-- Multi data source configuration database 1 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.test.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- Multi data source configuration database 2 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.test1.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"></property>
    </bean>

Configuration of complete applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--data source-Basic information of linked database,It's written directly here,Not put*.properties In resource file-->
    <!-- Test environment database 1 -->
	<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
       <property name="url" value="jdbc:oracle:thin:@172.83.242.145:1521:orcl" />
        <property name="username" value="ZSYW_JR" />
        <property name="password" value="ZSYW_JR" />
    </bean>
    <!-- Test environment database 2 -->
    <bean id="dataSource2"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
       <property name="url" value="jdbc:oracle:thin:@172.83.30.209:1521:orcl" />
        <property name="username" value="ydpt" />
        <property name="password" value="ydpt" />
    </bean>
    
    <!-- Configure data sources,load configuration,that is dataSource -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--mybatis Profile for-->
        <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <!--scanning XXXmapper.xml Mapping file,Configure path for scan-->
        <!-- <property name="mapperLocations" value="classpath:com/javafeng/mapping/*.xml"></property>
        <property name="mapperLocations" value="classpath:com/inspur/NSRZCXXGR/mapping/*.xml"></property> -->
        <property name="mapperLocations">
  			<array>
   				<value>classpath:com/inspur/SSO/Mapper/*.xml</value>
   				<value>classpath:com/inspur/test/dao/*.xml</value>
   				<value>classpath:com/inspur/NSRZCXXGR/mapping/*.xml</value>
 		    </array>
	    </property>
    </bean>
    
     <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource2"></property>
        <!--mybatis Profile for-->
        <property name="configLocation" value="classpath:/mybatis-config.xml" />
        <!--scanning XXXmapper.xml Mapping file,Configure path for scan-->
        <!-- <property name="mapperLocations" value="classpath:com/javafeng/mapping/*.xml"></property>
        <property name="mapperLocations" value="classpath:com/inspur/NSRZCXXGR/mapping/*.xml"></property> -->
        <property name="mapperLocations">
  			<array>
   				<value>classpath:com/inspur/test1/dao/*.xml</value>
 		    </array>
	    </property>
    </bean>
    
    
    <!-- DAO The package name of the interface, Spring Will automatically find the class in -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.NSRZCXXGR.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.SSO.IDao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- Multi data source configuration database 1 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.test.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- Multi data source configuration database 2 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.inspur.test1.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"></property>
    </bean>

    <!--transaction management-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--injection dataSource-->
        <property name="dataSource" ref="dataSource" />
        
    </bean>
    <!--Enable transaction annotation scanning-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

Test testController:

package com.inspur.test.controller;

import com.alibaba.fastjson.JSON;
import com.inspur.SSO.entity.token;
import com.inspur.SSO.service.tokenService;
import com.inspur.test.entity.test;
import com.inspur.test.service.test1Service;
import com.inspur.test.service.testService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 13926 on 2017/7/18.
 */
@Controller
@RequestMapping(value = "/test")
public class testController {
    @Resource(name = "test1Service")
    test1Service t1Service;
    
    @Resource(name = "testService")
    testService tService;

    @RequestMapping(value="/add", method=RequestMethod.POST)
    @ResponseBody
	public  JSON queryTokenYX(String id,String name) {
    	
    	test tt = new test();
    	
    	tt.setId(id);
    	
    	tt.setName(name);
		
		t1Service.insert(tt);//Add test data to database 1
		
		tService.insert(tt);//Add test data to database 2
		
		
		Map<String, Object> result = new HashMap<String, Object>();  
		
		result.put("flag", "1");
		result.put("success", "The Lost Tomb");
		result.put("shxxdm", "23435345345465");
		result.put("SSJT", "What country is my hometown");
		
		System.out.println("Returned JSON strand"+ JSON.toJSON(result));
		
		JSON jsonString = (JSON) JSON.toJSON(result);  
		
		return jsonString;
		
	}
    
}

Call Http interface:

 

Database data:

Data source 1:

Database 2

Posted by richo89 on Tue, 31 Dec 2019 20:49:13 -0800