配置多数据源

一. 修改启动类

  • 屏蔽“DataSourceAutoConfiguration.class”,因为它会读取application.properties文件的spring.datasource.*属性并自动配置单数据源
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
    
  • 删除原有扫描路径,后续为每个数据源单独配置
    @MapperScan(basePackages = {"com.example.dao"})
    

二. 配置application.properties多数据源

  • spring.datasource后插入别名,以区分不同数据源
# MySQL数据源1驱动
spring.datasource.database1.driver-class-name=com.mysql.jdbc.Driver
# MySQL数据源1路径
spring.datasource.database1.jdbc-url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useUnicode=true&serverTimezone=UTC
# MySQL登陆账户1
spring.datasource.database1.username=root
# MySQL登陆密码1
spring.datasource.database1.password=123456

# MySQL数据源2驱动
spring.datasource.database2.driver-class-name=com.mysql.jdbc.Driver
# MySQL数据源2路径
spring.datasource.database2.jdbc-url=jdbc:mysql://localhost:3306/scp?characterEncoding=utf-8&useUnicode=true&serverTimezone=UTC
# MySQL登陆账户2
spring.datasource.database2.username=root
# MySQL登陆密码2
spring.datasource.database2.password=123456

三. 添加多数据源配置:DataSourceConfig

package com.example.config;

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 javax.sql.DataSource;

/**
 * @author :zhangbocong
 * @version :V1.0
 * @program :spring_boot_demo
 * @date :Created in 2021年1月29日 10:18
 * @description :多数据源配置
 */
@Configuration
public class DataSourceConfig {
    //数据源1
    @Bean(name = "database1")
    @ConfigurationProperties(prefix = "spring.datasource.database1") 
    public DataSource businessDbDataSource() {
        return DataSourceBuilder.create().build();
    }
    //数据源2
    @Bean(name = "database2")
    @ConfigurationProperties(prefix = "spring.datasource.database2")
    public DataSource newHomeDbDataSource() {
        return DataSourceBuilder.create().build();
    }
}

四. 配个所有数据源

  • 配置数据源1:DataBaseOneConfig
package com.example.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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author :zhangbocong
 * @version :V1.0
 * @program :spring_boot_demo
 * @date :Created in 2021年1月29日 10:25
 * @description :数据源1配置
 */
@Configuration
@MapperScan(basePackages = {"com.example.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryDb1")
public class DataBaseOneConfig {
    @Autowired
    @Qualifier("database1")
    private DataSource dataSourceDb1;

    @Bean
    public SqlSessionFactory sqlSessionFactoryDb1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceDb1);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/mapper/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDb1());
    }
}
  • 配置数据源2:DataBaseTwoConfig
package com.example.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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author :zhangbocong
 * @version :V1.0
 * @program :spring_boot_demo
 * @date :Created in 2021年1月29日 10:29
 * @description :数据源2配置
 */
@Configuration
@MapperScan(basePackages = {"com.example.dao"}, sqlSessionFactoryRef = "sqlSessionFactoryDb2")
public class DataBaseTwoConfig {
    @Autowired
    @Qualifier("database2")
    private DataSource dataSourceDb2;


    @Bean
    public SqlSessionFactory sqlSessionFactoryDb2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceDb2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/mapper/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryDb2());
    }
}

五. 启动测试

参考地址

https://www.cnblogs.com/rude3knife/p/13562468.html