spring boot 集成 dynamic-datasource-spring-boot-starter

spring boot 集成 dynamic-datasource-spring-boot-starter

在使用多数据源时,为了不再造轮子,就使用 mybatis-plus 中的 dynamic-datasource-spring-boot-starter

一般集成直接看官网就行 dynamic-datasource-spring-boot-starter

场景

在非多数据源的项目基础上加集成多数据源

现在要讲的是通过 java 代码方式 实现

引入依赖

xml 复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>4.3.0</version>
</dependency>

// 用户 postgresql
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

配置Properties

application.properties:

properties 复制代码
spring.datasource.password=admin
spring.datasource.platform=mysql
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://192.168.x.x:3306/system?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=admin
# 驱动类型需要!!
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# slave1 postgresql
spring.datasource.slave1.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.slave1.password=admin
spring.datasource.slave1.url=jdbc:postgresql://192.168.x.x:5432/user
spring.datasource.slave1.username=admin
spring.datasource.slave1.driverClassName=org.postgresql.Driver
# 指定多数据源默认数据库
spring.datasource.dynamic.primary=master


# slave2 mysql
spring.datasources.slave2.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasources.slave2.jdbcUrl=jdbc:mysql://192.168.x.x:3306/test?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasources.slave2.username=admin
spring.datasources.slave2.password=admin

DataSourceConfig:

java 复制代码
@Configuration
public class DataSourceConfig {

    //原始的数据源
    @Bean(name = "master")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSourceProperty masterDataSourceProperty() {
        return new DataSourceProperty();
    }

    
    @Bean(name = "slave1")
    @ConfigurationProperties(prefix = "spring.datasource.slave1")
    public DataSourceProperty slave1DataSourceProperty() {
        return new DataSourceProperty();
    }

    @Bean(name = "slave2")
    @ConfigurationProperties(prefix = "spring.datasource.slave2")
    public DataSourceProperty slave2DataSourceProperty() {
        return new DataSourceProperty();
    }


    @Bean
    @Order(1)
    public DynamicDataSourceProvider beanDataSourceProvider(DefaultDataSourceCreator defaultDataSourceCreator) {
        Map<String, DataSourceProperty> dataSourceMap = new HashMap<>();
        dataSourceMap.put("slave1", slave1DataSourceProperty());
        dataSourceMap.put("master", masterDataSourceProperty());
        dataSourceMap.put("slave2", slave2DataSourceProperty());
        return new YmlDynamicDataSourceProvider(defaultDataSourceCreator, dataSourceMap);
    }
}

使用

java 复制代码
@DS("slave1")
@Override
public List<User> queryPage(int offset, int pageSize) {
    //分页
    return baseMapper.queryList(offset,pageSize);
}

good luck!

相关推荐
宠友信息7 小时前
多端数据互通场景下Spring Boot仿小红书源码结构设计
数据库·spring boot·redis·缓存·架构
不会c+7 小时前
02-SpringBoot配置文件
java·spring boot·后端
格子软件10 小时前
2026年GEO优化系统源码解构:核心状态机与高并发流控深度剖析
java·vue.js·spring boot·vue·geo
Flittly11 小时前
【AgentScope Java新手村系列】(17)长期记忆系统
java·spring boot·spring
SeeYa-J12 小时前
Sprint 1-2:创建第一个 Spring Boot Module(user-service)
java·spring boot·sprint
尚早立志14 小时前
Spring Boot 源码研读之ConfigurableEnvironment 环境准备
java·spring boot·后端
杨运交18 小时前
[043][数据模块]基于 Spring Data JPA 的企业级数据访问层设计——实体、审计、状态与服务抽象
spring boot
飘灬渺18 小时前
Spring Boot :将自定义的 JAR 包引入到 Maven 项目中
spring boot·maven·jar
格子软件19 小时前
格子GEO优化系统源码深度解析:从零搭建AI驱动的内容矩阵
java·vue.js·人工智能·spring boot·vue·geo
Flittly4 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring