springboot配置多数据源

目录结构
springboot配置多数据源_第1张图片

需要的maven依赖

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.1.10</version>
</dependency>

application.properties

spring: 
  datasource: 
    first: 
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://ip:端口/数据库名?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT
      username: ccccoi
      password: zjht@2019
    second: 
      driver-class-name: com.mysql.jdbc.Driver
      jdbc-url: jdbc:mysql://ip:端口/数据库名?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT
      username: root
      password: 123456

DataSourceConfig1

package cn.ccccltd.ccccoi.base.config;

import javax.sql.DataSource;

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;

//表示这个类为一个配置类
@Configuration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "cn.ccccltd.ccccoi.**.dao", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class DataSourceConfig1 {
  // 将这个对象放入Spring容器中
  @Bean(name = "firstDataSource")
  // 表示这个数据源是默认数据源
  @Primary
  // 读取application.properties中的配置参数映射成为一个对象
  // prefix表示参数的前缀
  @ConfigurationProperties(prefix = "spring.datasource.first")
  public DataSource getDateSource1() {
      return DataSourceBuilder.create().build();
  }
  @Bean(name = "firstSqlSessionFactory")
  // 表示这个数据源是默认数据源
  @Primary
  // @Qualifier表示查找Spring容器中名字为firstDataSource的对象
  public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource datasource)
          throws Exception {
      SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
      bean.setDataSource(datasource);
      bean.setMapperLocations(
              // 设置mybatis的xml所在位置
              new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml"));
      return bean.getObject();
  }
  @Bean("firstSqlSessionTemplate")
  // 表示这个数据源是默认数据源
  @Primary
  public SqlSessionTemplate firstsqlsessiontemplate(
          @Qualifier("firstSqlSessionFactory") SqlSessionFactory sessionfactory) {
      return new SqlSessionTemplate(sessionfactory);
  }
}

DataSourceConfig2

package cn.ccccltd.ccccoi.base.config;

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

@Configuration
@MapperScan(basePackages = "cn.ccccltd.ccccoi.mediumterminvestment.projectfilling.test.mapper", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataSourceConfig2 {
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/test/*.xml"));
        return bean.getObject();
    }
    @Bean("secondSqlSessionTemplate")
    public SqlSessionTemplate secondsqlsessiontemplate(
            @Qualifier("secondSqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

注:配置两个数据源需要配置两个config,通过@MapperScan(basePackages = “”)的不同,和配置xml路径的不同,来确定什么时候用什么数据源。

配置&serverTimezone=GMT防止报数据库时区错误

你可能感兴趣的:(springboot配置多数据源)