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!

相关推荐
学长毕业设计27 分钟前
基于SpringBoot的民间艺术传承管理系统(源码+文档+讲解视频)
java·spring boot·后端
小蒜学长27 分钟前
基于Springboot+Vue的环保行动志愿者招募系统设计与实现(代码+数据库+LW)
java·数据库·vue.js·spring boot·后端
Json____2 小时前
springboot开发高校选课管理系统:从零构建前后端分离的全栈实践
java·spring boot·后端·毕业设计·毕设·wwwoop.com
xcl09252 小时前
民宿预约系统开发实战:从需求分析到上线部署全流程解析
java·spring boot
步行cgn2 小时前
为何以继承方式引入SpringBoot
java·spring boot·后端
wno7043 小时前
Spring Boot配合Hibernate Validator参数校验
spring boot·后端·hibernate
边境悍匪5 小时前
蜗牛学苑 Java 智能体学习 Day34|AOP 进阶、权限思维导图复盘
java·开发语言·spring boot·学习
huaweichenai5 小时前
spring boot 实现数据库分页操作
数据库·spring boot
摇滚侠5 小时前
《SpringBoot 3:入门与应用实战》第 13 章 整合 MyBatis MyBatis 概述 阅读笔记 37
spring boot·笔记·mybatis
vipxieliang5 小时前
订单数据验证:从购物车到支付的完整验证链路
java·spring boot