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);
    }
}
相关推荐
GoGeekBaird10 分钟前
Andrej Karpathy:2025年大模型发展总结
后端·github
uzong24 分钟前
听一听技术面试官的心路历程:他们也会有瓶颈,也会表现不如人意
后端
Jimmy25 分钟前
年终总结 - 2025 故事集
前端·后端·程序员
吴佳浩 Alben2 小时前
Python入门指南(四)
开发语言·后端·python
倚栏听风雨2 小时前
lombook java: 找不到符号
后端
码财小子3 小时前
记一次服务器大并发下高延迟问题的定位
后端
我是小妖怪,潇洒又自在3 小时前
springcloud alibaba(九)Nacos Config服务配置
后端·spring·spring cloud
Victor3563 小时前
Netty(26)如何实现基于Netty的RPC框架?
后端
Victor3563 小时前
Netty(25)Netty的序列化和反序列化机制是什么?
后端