1. 程式人生 > >Spring boot 2.0+ MyBatis 多資料來源(多Mysql)

Spring boot 2.0+ MyBatis 多資料來源(多Mysql)

業務的需求需要兩個在不同伺服器的資料庫。

首先確定Springboot版本,我這裡引用的是2.0.0

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

所以application.yml檔案資料庫配置為

spring:
 datasource:
  xx1:
    driver-class-name: com.mysql.jdbc.Driver
    initial-size: 5
    max-idle: 10
    max-wait: 10000
    min-idle: 5
    password: 123456
    jdbc-url: jdbc:mysql://xxx.x.x.x:xxxx/xx1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
  xx2:
    driver-class-name: com.mysql.jdbc.Driver
    initial-size: 5
    max-idle: 10
    max-wait: 10000
    min-idle: 5
    password: 123456
    jdbc-url: jdbc:mysql://xxx.x.x.x:xxxx/xx2?useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root


注意:url:  -----> jdbc-url:,如果不改的會有Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.然後將dao層的mapper介面分為兩個資料庫的mapper介面

同樣xml檔案也分為兩個(忽略我的第二個檔案暫時為空)

package com.unicom.dsp.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Created by jiangxiaoyi on 2018/12/12.
 */
@Configuration
@MapperScan(basePackages = {"com.unicom.dsp.dao.amapper"},sqlSessionTemplateRef = "xx1SqlSessionTemplate")
public class MybatisDbAConfig {

    @Bean(name = "xx1Datasource")
    @ConfigurationProperties(prefix = "spring.datasource.xx1")
    @Primary
    public DataSource xx1DataDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "xx1SqlSessionFactory")
    @Primary
    public SqlSessionFactory xx1SqlSessionFactory(@Qualifier("xx1Datasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/amapping/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "xx1TransactionManger")
    @Primary
    public DataSourceTransactionManager xx1TransactionManger(@Qualifier("xx1Datasource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "xx1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate xx1SqlSessionTemplate(@Qualifier("xx1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

 

 

package com.unicom.dsp.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Created by jiangxiaoyi on 2018/12/12.
 */
@Configuration
@MapperScan(basePackages = {"com.unicom.dsp.dao.bmapper"},sqlSessionTemplateRef = "xx2SqlSessionTemplate")
public class MybatisDbBConfig {

    @Bean(name = "xx2Datasource")
    @ConfigurationProperties(prefix = "spring.datasource.xx2")
    public DataSource xx2Datasource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "xx2SqlSessionFactory")
    public SqlSessionFactory xx2SqlSessionFactory(@Qualifier("xx2Datasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/bmapping/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "xx2TransactionManger")
    public DataSourceTransactionManager xx2TransactionManger(@Qualifier("xx2Datasource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "xx2SqlSessionTemplate")
    public SqlSessionTemplate xx2SqlSessionTemplate(@Qualifier("xx2SqlSessionFactory")SqlSessionFactory sqlSessionFactory) throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }

注意classpath後面的*。

希望能幫助到你~如有問題可以留言哦~希望大家一起學習