SpringBoot学习(15)— 配置多个数据源

介绍

在项目开发中,经常会遇到数据来源于几个数据库

如何配置

springboot配置文件 application.yml

配置多个

spring:
  datasource:
    esystem:
      driverClassName: com.mysql.cj.jdbc.Driver
      #单数据源此处为url,多数据源需改成jdbc-url,否则会报错
      jdbc-url: jdbc:mysql://localhost:3306/esystem?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
      username: root
      password: root

    log:
      driverClassName: com.mysql.cj.jdbc.Driver
      #url: jdbc:mysql://139.155.11.159:3306/j-demo?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
      jdbc-url: jdbc:mysql://192.168.30.220:3306/log?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
      username: maxscale_route
      password: U5lW0HGg1

创建配置文件

启动的时候注入

  • 第一个(主数据库)数据源的配置文件
package com.eceibs.report.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.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.eceibs.report.mapper.esystem",sqlSessionTemplateRef ="esystemSqlSessionTemplate")
public class EsystemDataSourceConfig {
    // @Primary 确定此数据源为master
    @Bean(name = "esystemDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.esystem")
    @Primary
    public DataSource esystemDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "esystemSqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("esystemDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    //配置事务管理器
    @Bean(name = "esystemTransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("esystemDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "esystemSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("esystemSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
  • 第二个数据源的配置文件
package com.eceibs.report.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.eceibs.report.mapper.log",sqlSessionTemplateRef ="logSqlSessionTemplate")
public class LogDataSourceConfig {
    // @Primary 确定此数据源为master
    @Bean(name = "logDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.log")
    public DataSource logDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "logSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("logDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    //配置事务管理器
    @Bean(name = "logTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("logDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "logSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("logSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

修改mapper路径

image.png

这样可以区分mapper对应的数据源。

你可能感兴趣的:(SpringBoot学习(15)— 配置多个数据源)