SpringBoot配置多数据源

1.yml

yml 复制代码
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    primary:(自定义名称)
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: xxxxx
      username: xxxxxx
      password: xxxxx
    secondary:(自定义名称)
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: xxxxx
      username: xxxxx
      password: xxxxx
    druid:
      #配置初始化大小/最小/最大
      initial-size: 1
      min-idle: 1
      max-active: 20
      #获取连接等待超时时间
      max-wait: 60000

2.配置文件 Config

Java 复制代码
package cn.net.hanmu.quickDF.config;

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

/**
 * @Author: 
 * @Date: 2023/12/18 14:27
 * @Description: 多数据源配置中心
 * @Version: v1.0
 */
@Configuration
public class DataSourceConfig {
    /**
     * 基础数据库    @Primary表示主数据源
     * @return
     */
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 第二个数据库
     * @return
     */
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

数据源配置类

java 复制代码
package cn.net.hanmu.quickDF.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Author: 
 * @Date: 2023/12/18 14:35
 * @Description:
 * @Version: v1.0
 */
@Configuration
@MapperScan(basePackages = "cn.net.hanmu.quickDF.*.dao", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryConfig {

    /**
     * sqlSession创建工厂配置类
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "primarySqlSessionFactory")
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        //注入数据库配置
        bean.setDataSource(dataSource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*/*.xml"));//mapper文件扫描文件位置
        return bean.getObject();
    }


    /**
     * 将sqlSession传递给spring boot
     * @param sqlSessionFactory
     * @return
     */
    @Bean(name = "primarySqlSessionTemplate")
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
java 复制代码
package cn.net.hanmu.quickDF.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Author: 
 * @Date: 2023/12/18 14:46
 * @Description: 第二数据源
 * @Version: v1.0
 */
@Configuration
@MapperScan(basePackages = "cn.net.hanmu.quickDF.BisicDao", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryConfig {
    /**
     * sqlSession创建工厂配置类
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);//注入数据库配置
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:bisicMapper/*.xml"));//mappper文件扫描文件位置
        return bean.getObject();
    }

    /**
     * 将sqlSession传递给spring boot
     *
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean(name = "secondarySqlSessionTemplate")
    public SqlSessionTemplate secondarySqlSessionTemplate(
            @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
相关推荐
muxue1787 分钟前
go:运行第一个go语言程序
开发语言·后端·golang
米饭好好吃.8 分钟前
【Go】Go wire 依赖注入
开发语言·后端·golang
闲猫8 分钟前
go 接口interface func (m Market) getName() string {
开发语言·后端·golang
Good Note8 分钟前
Golang的静态强类型、编译型、并发型
java·数据库·redis·后端·mysql·面试·golang
可爱de艺艺8 分钟前
Go入门之struct
开发语言·后端·golang
信徒_11 分钟前
Go 语言中的协程
开发语言·后端·golang
m0_7482365824 分钟前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
web1511736022330 分钟前
Spring Boot项目中解决跨域问题(四种方式)
spring boot·后端·dubbo
我就是我35244 分钟前
记录一次SpringMVC的406错误
java·后端·springmvc
过客猫20223 小时前
使用 deepseek实现 go语言,读取文本文件的功能,要求支持 ascii,utf-8 等多种格式自适应
开发语言·后端·golang