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);
    }
}
相关推荐
你的人类朋友8 小时前
说说签名与验签
后端
databook8 小时前
Manim实现脉冲闪烁特效
后端·python·动效
canonical_entropy12 小时前
AI时代,我们还需要低代码吗?—— 一场关于模型、演化与软件未来的深度问答
后端·低代码·aigc
颜如玉13 小时前
HikariCP:Dead code elimination优化
后端·性能优化·源码
考虑考虑13 小时前
Jpa使用union all
java·spring boot·后端
bobz96514 小时前
virtio vs vfio
后端
Rexi15 小时前
“Controller→Service→DAO”三层架构
后端
bobz96515 小时前
计算虚拟化的设计
后端
深圳蔓延科技15 小时前
Kafka的高性能之路
后端·kafka
Barcke15 小时前
深入浅出 Spring WebFlux:从核心原理到深度实战
后端