spring boot 集成 mysql ,mybatisplus多数据源

1、需要的依赖,版本自行控制

cs 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>

2、配置文件

cs 复制代码
spring:
  datasource:
    dynamic:
      primary: master
      strict: false
      datasource:
        master:
          url: jdbc:mysql://xxx?useSSL=false&useUnicode=true&characterEncoding=utf8
          username: root
          password: pas
          driver-class-name: com.mysql.cj.jdbc.Driver
        two:
          url: jdbc:mysql://xxx?useSSL=false&useUnicode=true&characterEncoding=utf8
          username: root
          password: pas
          driver-class-name: com.mysql.cj.jdbc.Driver

3、使用方法,添加DS注解,值为配置的数据源名称

4、直接使用

相关推荐
一起_java9 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
Flynn Liang11 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
索西引擎12 小时前
MySQL 进阶:视图、存储过程、存储函数与触发器
数据库·mysql
bksczm14 小时前
InnoDB 通用表空间与临时表空间详解
大数据·数据库·mysql
靠沿14 小时前
MySQL数据库进阶——存储过程和触发器
java·数据库·mysql
码少女15 小时前
MySQL--库与表的增删改查基础
java·数据库·mysql
打工的小王16 小时前
postgresql和mysql对比
数据库·mysql·postgresql
Flynt17 小时前
掘金热文说 Spring Boot 4.1 默认开虚拟线程,我实测后发现了更狠的坑
java·spring boot·性能优化
codeejun19 小时前
每日一Go·MySQL-15、慢查询日志实战分析(从 0 到 1 实战排查慢 SQL)
sql·mysql·golang
Decay__20 小时前
SQLServer 监控体系构建 —— sql_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·mysql·架构