多数据源配置 If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).
时间: 2025-06-17 17:48:16 浏览: 8
### 多数据源配置及嵌入式数据库的 Classpath 设置与 Profile 激活
在 Spring Boot 中,多数据源配置可以通过 `application.properties` 或 `application.yml` 文件实现。以下是一个完整的解决方案,包括嵌入式数据库(如 H2、HSQL 或 Derby)的 Classpath 设置以及 Profile 激活。
#### 1. 嵌入式数据库的 Classpath 设置
为了使用嵌入式数据库(如 H2、HSQL 或 Derby),需要将相应的依赖添加到项目的 classpath 中。例如,对于 H2 数据库,可以在 `pom.xml` 中添加以下依赖:
```xml
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
```
对于 HSQL 或 Derby 数据库,可以分别添加以下依赖:
```xml
<!-- HSQL Database -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Apache Derby -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
```
这些依赖确保了嵌入式数据库能够在运行时加载[^2]。
#### 2. 配置多数据源
Spring Boot 支持通过 `application.properties` 或 `application.yml` 配置多个数据源。以下是一个示例,展示如何配置两个数据源(主数据源和从数据源):
**application.properties 示例:**
```properties
# 主数据源配置
spring.datasource.primary.driver-class-name=org.h2.Driver
spring.datasource.primary.url=jdbc:h2:mem:testdb
spring.datasource.primary.username=sa
spring.datasource.primary.password=password
# 从数据源配置
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=******
```
**application.yml 示例:**
```yaml
spring:
datasource:
primary:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
username: sa
password: password
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: ******
```
每个数据源都需要一个独立的配置,包括驱动程序、URL、用户名和密码[^1]。
#### 3. Profile 激活
为了根据不同的环境(如开发环境 `dev`、测试环境 `test` 和生产环境 `prod`)激活不同的配置文件,可以使用 Spring Boot 的 Profile 功能。以下是一个示例:
**application-dev.properties 示例:**
```properties
spring.profiles.active=dev
spring.datasource.primary.url=jdbc:h2:mem:testdb_dev
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test_dev?useUnicode=true&characterEncoding=utf8&useSSL=false
```
**application-prod.properties 示例:**
```properties
spring.profiles.active=prod
spring.datasource.primary.url=jdbc:h2:mem:testdb_prod
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test_prod?useUnicode=true&characterEncoding=utf8&useSSL=false
```
通过命令行参数或系统属性激活特定的 Profile:
```bash
java -Dspring.profiles.active=dev -jar your-application.jar
```
或者在 `application.properties` 中直接指定:
```properties
spring.profiles.active=dev
```
#### 4. 嵌入式数据库的自动初始化
嵌入式数据库(如 H2)可以通过 `schema.sql` 和 `data.sql` 文件进行初始化。将这些文件放置在 `src/main/resources` 目录下,Spring Boot 会在启动时自动执行这些脚本[^3]。
**schema.sql 示例:**
```sql
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
```
**data.sql 示例:**
```sql
INSERT INTO users (id, name) VALUES (1, 'John Doe');
```
#### 5. 相关代码示例
以下是一个简单的 Java 配置类,用于定义多数据源:
```java
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 javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
```
以上代码展示了如何通过 `@ConfigurationProperties` 注解绑定配置文件中的属性到数据源对象[^4]。
---
###
阅读全文
相关推荐












