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后面的*。

希望能帮助到你~如有问题可以留言哦~希望大家一起学习

猜你喜欢

转载自blog.csdn.net/jiangxiaoyi_07/article/details/84982875
今日推荐