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);
    }
}
相关推荐
桦说编程5 小时前
深入解析CompletableFuture源码实现(2)———双源输入
java·后端·源码
舒一笑5 小时前
大模型时代的程序员成长悖论:如何在AI辅助下不失去竞争力
后端·程序员·掘金技术征文
lang201509285 小时前
Spring Boot优雅关闭全解析
java·spring boot·后端
小羊在睡觉6 小时前
golang定时器
开发语言·后端·golang
用户21411832636026 小时前
手把手教你在魔搭跑通 DeepSeek-OCR!光学压缩 + MoE 解码,97% 精度还省 10-20 倍 token
后端
追逐时光者6 小时前
一个基于 .NET 开源、功能强大的分布式微服务开发框架
后端·.net
刘一说7 小时前
Spring Boot 启动慢?启动过程深度解析与优化策略
java·spring boot·后端
壹佰大多7 小时前
【spring如何扫描一个路径下被注解修饰的类】
java·后端·spring
间彧7 小时前
Java双亲委派模型的具体实现原理是什么?
后端
间彧7 小时前
Java类的加载过程
后端