Configuring Multiple Data Sources in Spring Boot
11/11/21Less than 1 minute
1. Development Environment:
Spring Boot 2.2.12.RELEASE, MySQL 8.0.2, PostgreSQL 13.2
2. Install Maven Dependencies
- MySQL Dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>- PostgreSQL Dependency
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>- Multiple Data Sources Configuration Dependency
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>3. Add Configuration Information

4. Usage
For querying the MySQL database, simply add the @DS("master") annotation to the mapper.
Original Article Link: https://blog.csdn.net/qq_32259239/article/details/121266278
AI Translation | AI 翻译
This article was translated from Chinese to English by AI. If there are any inaccuracies, please refer to the original Chinese version.
本文由 AI 辅助从中文翻译为英文。如遇不准确之处,请以中文原版为准。
