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);
    }
}
相关推荐
BXCQ_xuan8 分钟前
软件工程实践二:Spring Boot 知识回顾
java·spring boot·后端
o0o_-_26 分钟前
【go/gopls/mcp】官方gopls内置mcp server使用
开发语言·后端·golang
苏三说技术39 分钟前
为什么不建议在 Docker 中跑 MySQL?
后端
二饭40 分钟前
Spring Boot 项目启动报错:MongoSocketOpenException 连接被拒绝排查日记
java·spring boot·后端
荣达1 小时前
koa洋葱模型理解
前端·后端·node.js
AAA修煤气灶刘哥3 小时前
Kafka 入门不踩坑!从概念到搭环境,后端 er 看完就能用
大数据·后端·kafka
月小水长3 小时前
大模型接入自定义 MCP Server,我开发了个免费使用的基金涨跌归纳和归因分析的 Agent
人工智能·后端
yinke小琪3 小时前
说说hashCode() 和 equals() 之间的关系
java·后端·面试
花果山最Man的男人3 小时前
@Autowired注解使用说明
后端