fix(h2): honor spring.datasource settings and preserve proxy-aware H2 console redirects
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
package com.example.scaffold.config.database;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多数据库配置 - H2 / MySQL / PostgreSQL
|
||||||
|
*
|
||||||
|
* 学习要点:
|
||||||
|
* 1. @Profile - 根据环境选择配置
|
||||||
|
* 2. 数据源配置
|
||||||
|
* 3. 连接池配置
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Configuration
|
||||||
|
public class DatabaseConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* H2 内存数据库 - 开发测试用(默认)
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Profile("!mysql & !postgresql")
|
||||||
|
@ConditionalOnMissingBean(DataSource.class)
|
||||||
|
public DataSource h2DataSource(
|
||||||
|
@Value("${spring.datasource.url:jdbc:h2:mem:springboot_scaffold_learn;MODE=MYSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE}") String url,
|
||||||
|
@Value("${spring.datasource.username:sa}") String username,
|
||||||
|
@Value("${spring.datasource.password:}") String password) {
|
||||||
|
log.info("🚀 使用 H2 数据库: {}", url);
|
||||||
|
return DataSourceBuilder.create()
|
||||||
|
.driverClassName("org.h2.Driver")
|
||||||
|
.url(url)
|
||||||
|
.username(username)
|
||||||
|
.password(password)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MySQL 数据库
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Profile("mysql")
|
||||||
|
public DataSource mysqlDataSource(
|
||||||
|
@Value("${DB_URL:jdbc:mysql://localhost:3306/springboot_scaffold}") String url,
|
||||||
|
@Value("${DB_USER:root}") String username,
|
||||||
|
@Value("${DB_PASS:}") String password) {
|
||||||
|
log.info("🚀 使用 MySQL 数据库: {}", url);
|
||||||
|
return DataSourceBuilder.create()
|
||||||
|
.driverClassName("com.mysql.cj.jdbc.Driver")
|
||||||
|
.url(url)
|
||||||
|
.username(username)
|
||||||
|
.password(password)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostgreSQL 数据库
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Profile("postgresql")
|
||||||
|
public DataSource postgresqlDataSource(
|
||||||
|
@Value("${DB_URL:jdbc:postgresql://localhost:5432/springboot_scaffold}") String url,
|
||||||
|
@Value("${DB_USER:postgres}") String username,
|
||||||
|
@Value("${DB_PASS:}") String password) {
|
||||||
|
log.info("🚀 使用 PostgreSQL 数据库: {}", url);
|
||||||
|
return DataSourceBuilder.create()
|
||||||
|
.driverClassName("org.postgresql.Driver")
|
||||||
|
.url(url)
|
||||||
|
.username(username)
|
||||||
|
.password(password)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ server.forward-headers-strategy=framework
|
|||||||
|
|
||||||
spring.profiles.active=${APP_PROFILE:learn}
|
spring.profiles.active=${APP_PROFILE:learn}
|
||||||
|
|
||||||
# H2 Database (default for learning)
|
# H2 Database (default baseline; learn profile overrides to in-memory)
|
||||||
spring.h2.console.enabled=true
|
spring.h2.console.enabled=true
|
||||||
spring.datasource.url=jdbc:h2:file:~/h2/springboot_scaffold
|
spring.datasource.url=jdbc:h2:file:~/h2/springboot_scaffold
|
||||||
spring.datasource.driverClassName=org.h2.Driver
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
|||||||
Reference in New Issue
Block a user