generated from Java-2025Fall/final-vibevault-template
Compare commits
No commits in common. "531de40282578e7dca9ec5c9e24fd7605e0e166a" and "0b73de2ee6b36fed3005480b2e18ce5b716f5662" have entirely different histories.
531de40282
...
0b73de2ee6
@ -1,89 +0,0 @@
|
||||
# VibeVault 数据库连接配置
|
||||
|
||||
## 远程数据库连接信息
|
||||
|
||||
### 生产环境配置
|
||||
- **数据库类型**: PostgreSQL
|
||||
- **主机地址**: 49.234.193.192
|
||||
- **端口**: 5432
|
||||
- **数据库名**: vibevault
|
||||
- **用户名**: postgres
|
||||
- **密码**: postgres
|
||||
|
||||
### 配置文件说明
|
||||
|
||||
#### 1. 开发环境 (application.properties)
|
||||
默认使用H2内存数据库,适合本地开发和测试:
|
||||
```properties
|
||||
# 开发/测试时使用 H2 内存数据库
|
||||
spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
```
|
||||
|
||||
#### 2. 生产环境 (application-prod.properties)
|
||||
使用远程PostgreSQL数据库:
|
||||
```properties
|
||||
# 生产环境使用 PostgreSQL 远程数据库
|
||||
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
```
|
||||
|
||||
### 环境切换
|
||||
|
||||
#### 方式1: 启动参数
|
||||
```bash
|
||||
# 开发环境 (默认)
|
||||
./gradlew bootRun
|
||||
|
||||
# 生产环境
|
||||
./gradlew bootRun --args='--spring.profiles.active=prod'
|
||||
```
|
||||
|
||||
#### 方式2: 环境变量
|
||||
```bash
|
||||
# 设置环境变量
|
||||
set SPRING_PROFILES_ACTIVE=prod
|
||||
./gradlew bootRun
|
||||
```
|
||||
|
||||
#### 方式3: 系统属性
|
||||
```bash
|
||||
# 通过系统属性
|
||||
./gradlew bootRun -Dspring.profiles.active=prod
|
||||
```
|
||||
|
||||
### 数据库连接测试
|
||||
|
||||
项目包含数据库连接测试类 `DatabaseConnectionTest`,可以验证与远程数据库的连接:
|
||||
|
||||
```bash
|
||||
# 运行数据库连接测试
|
||||
./gradlew test --tests DatabaseConnectionTest
|
||||
```
|
||||
|
||||
### 注意事项
|
||||
|
||||
1. **网络连接**: 确保可以访问远程数据库服务器 `49.234.193.192:5432`
|
||||
2. **防火墙**: 检查本地防火墙设置,确保5432端口未被阻止
|
||||
3. **数据库权限**: 确保数据库用户具有必要的读写权限
|
||||
4. **连接池**: Spring Boot会自动配置Hikari连接池
|
||||
|
||||
### 故障排除
|
||||
|
||||
如果连接失败,请检查:
|
||||
- 网络连接是否正常
|
||||
- 数据库服务是否正在运行
|
||||
- 用户名和密码是否正确
|
||||
- 数据库是否存在
|
||||
|
||||
### 支持的数据库操作
|
||||
|
||||
- ✅ 自动建表 (spring.jpa.hibernate.ddl-auto=update)
|
||||
- ✅ 事务管理
|
||||
- ✅ 连接池
|
||||
- ✅ JPA/Hibernate支持
|
||||
- ✅ 数据库迁移 (如果需要)
|
||||
@ -1,24 +0,0 @@
|
||||
# VibeVault Production Configuration
|
||||
spring.application.name=vibevault
|
||||
|
||||
# Production Database Configuration (PostgreSQL)
|
||||
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# JPA / Hibernate for Production
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
# JWT Configuration
|
||||
jwt.secret=your-production-secret-key-here-should-be-at-least-256-bits-long-for-hs256
|
||||
jwt.expiration=86400000
|
||||
|
||||
# Server
|
||||
server.port=8080
|
||||
|
||||
# Disable H2 Console in production
|
||||
spring.h2.console.enabled=false
|
||||
@ -1,13 +0,0 @@
|
||||
# Test Configuration - Uses H2 in-memory database
|
||||
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
|
||||
# Disable security for some tests (can be overridden per test)
|
||||
# spring.security.enabled=false
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
# VibeVault Application Configuration
|
||||
spring.application.name=vibevault
|
||||
|
||||
# Database Configuration
|
||||
# 开发/测试时使用 H2 内存数据库(每次重启清空数据)
|
||||
# spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
||||
# spring.datasource.username=sa
|
||||
# spring.datasource.password=
|
||||
# spring.datasource.driver-class-name=org.h2.Driver
|
||||
|
||||
# 生产环境使用 PostgreSQL 远程数据库
|
||||
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# JPA / Hibernate
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.show-sql=true
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
# H2 Console (访问 http://localhost:8080/h2-console 查看数据库)
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
|
||||
# JWT Configuration
|
||||
jwt.secret=your-secret-key-here-should-be-at-least-256-bits-long-for-hs256
|
||||
jwt.expiration=86400000
|
||||
|
||||
# Server
|
||||
server.port=8080
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,24 +0,0 @@
|
||||
# VibeVault Production Configuration
|
||||
spring.application.name=vibevault
|
||||
|
||||
# Production Database Configuration (PostgreSQL)
|
||||
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# JPA / Hibernate for Production
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
# JWT Configuration
|
||||
jwt.secret=your-production-secret-key-here-should-be-at-least-256-bits-long-for-hs256
|
||||
jwt.expiration=86400000
|
||||
|
||||
# Server
|
||||
server.port=8080
|
||||
|
||||
# Disable H2 Console in production
|
||||
spring.h2.console.enabled=false
|
||||
@ -3,16 +3,16 @@ spring.application.name=vibevault
|
||||
|
||||
# Database Configuration
|
||||
# 开发/测试时使用 H2 内存数据库(每次重启清空数据)
|
||||
# spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
||||
# spring.datasource.username=sa
|
||||
# spring.datasource.password=
|
||||
# spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
|
||||
# 生产环境使用 PostgreSQL 远程数据库
|
||||
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
# 如需使用 PostgreSQL,注释上面的 H2 配置,取消下面的注释
|
||||
# spring.datasource.url=jdbc:postgresql://localhost:5432/vibevault
|
||||
# spring.datasource.username=postgres
|
||||
# spring.datasource.password=postgres
|
||||
# spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# JPA / Hibernate
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
package com.vibevault;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* 数据库连接测试类
|
||||
* 用于验证与远程PostgreSQL数据库的连接
|
||||
*/
|
||||
@SpringBootTest(properties = {
|
||||
"spring.profiles.active=prod"
|
||||
})
|
||||
public class DatabaseConnectionTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Test
|
||||
void shouldConnectToDatabase() throws SQLException {
|
||||
// 测试数据源是否已正确配置
|
||||
assertThat(dataSource).isNotNull();
|
||||
|
||||
// 测试数据库连接
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
assertThat(connection).isNotNull();
|
||||
assertThat(connection.isValid(1000)).isTrue();
|
||||
|
||||
// 测试数据库类型
|
||||
String databaseProductName = connection.getMetaData().getDatabaseProductName();
|
||||
assertThat(databaseProductName).isEqualTo("PostgreSQL");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldExecuteQuery() {
|
||||
// 测试简单查询
|
||||
Integer result = jdbcTemplate.queryForObject("SELECT 1", Integer.class);
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCheckDatabaseVersion() {
|
||||
// 检查PostgreSQL版本
|
||||
String version = jdbcTemplate.queryForObject("SELECT version()", String.class);
|
||||
assertThat(version).contains("PostgreSQL");
|
||||
System.out.println("Database Version: " + version);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user