generated from Java-2025Fall/final-vibevault-template
Compare commits
2 Commits
0b73de2ee6
...
531de40282
| Author | SHA1 | Date | |
|---|---|---|---|
| 531de40282 | |||
| e2a2a72754 |
89
DATABASE_CONNECTION.md
Normal file
89
DATABASE_CONNECTION.md
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
# 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支持
|
||||||
|
- ✅ 数据库迁移 (如果需要)
|
||||||
24
bin/main/application-prod.properties
Normal file
24
bin/main/application-prod.properties
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# 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
|
||||||
13
bin/main/application-test.properties
Normal file
13
bin/main/application-test.properties
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# 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
|
||||||
|
|
||||||
31
bin/main/application.properties
Normal file
31
bin/main/application.properties
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# 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
|
||||||
BIN
bin/main/com/vibevault/VibeVaultApplication.class
Normal file
BIN
bin/main/com/vibevault/VibeVaultApplication.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/config/SecurityConfig.class
Normal file
BIN
bin/main/com/vibevault/config/SecurityConfig.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/AuthController.class
Normal file
BIN
bin/main/com/vibevault/controller/AuthController.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/LoginRequest.class
Normal file
BIN
bin/main/com/vibevault/controller/LoginRequest.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/LoginResponse.class
Normal file
BIN
bin/main/com/vibevault/controller/LoginResponse.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/PlaylistController.class
Normal file
BIN
bin/main/com/vibevault/controller/PlaylistController.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/RegisterRequest.class
Normal file
BIN
bin/main/com/vibevault/controller/RegisterRequest.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/controller/RegisterResponse.class
Normal file
BIN
bin/main/com/vibevault/controller/RegisterResponse.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/dto/PlaylistCreateDTO.class
Normal file
BIN
bin/main/com/vibevault/dto/PlaylistCreateDTO.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/dto/PlaylistDTO.class
Normal file
BIN
bin/main/com/vibevault/dto/PlaylistDTO.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/dto/SongCreateDTO.class
Normal file
BIN
bin/main/com/vibevault/dto/SongCreateDTO.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/dto/SongDTO.class
Normal file
BIN
bin/main/com/vibevault/dto/SongDTO.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/exception/GlobalExceptionHandler.class
Normal file
BIN
bin/main/com/vibevault/exception/GlobalExceptionHandler.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/exception/ResourceNotFoundException.class
Normal file
BIN
bin/main/com/vibevault/exception/ResourceNotFoundException.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/exception/UnauthorizedException.class
Normal file
BIN
bin/main/com/vibevault/exception/UnauthorizedException.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/model/Playlist.class
Normal file
BIN
bin/main/com/vibevault/model/Playlist.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/model/Song.class
Normal file
BIN
bin/main/com/vibevault/model/Song.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/model/User.class
Normal file
BIN
bin/main/com/vibevault/model/User.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/repository/PlaylistRepository.class
Normal file
BIN
bin/main/com/vibevault/repository/PlaylistRepository.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/repository/UserRepository.class
Normal file
BIN
bin/main/com/vibevault/repository/UserRepository.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/security/JwtAuthenticationFilter.class
Normal file
BIN
bin/main/com/vibevault/security/JwtAuthenticationFilter.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/security/JwtService.class
Normal file
BIN
bin/main/com/vibevault/security/JwtService.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/service/PlaylistService.class
Normal file
BIN
bin/main/com/vibevault/service/PlaylistService.class
Normal file
Binary file not shown.
BIN
bin/main/com/vibevault/service/PlaylistServiceImpl.class
Normal file
BIN
bin/main/com/vibevault/service/PlaylistServiceImpl.class
Normal file
Binary file not shown.
BIN
bin/test/com/vibevault/DatabaseConnectionTest.class
Normal file
BIN
bin/test/com/vibevault/DatabaseConnectionTest.class
Normal file
Binary file not shown.
BIN
bin/test/com/vibevault/PublicPlaylistServiceTest.class
Normal file
BIN
bin/test/com/vibevault/PublicPlaylistServiceTest.class
Normal file
Binary file not shown.
BIN
bin/test/com/vibevault/PublicSmokeTest.class
Normal file
BIN
bin/test/com/vibevault/PublicSmokeTest.class
Normal file
Binary file not shown.
24
src/main/resources/application-prod.properties
Normal file
24
src/main/resources/application-prod.properties
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# 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
|
# Database Configuration
|
||||||
# 开发/测试时使用 H2 内存数据库(每次重启清空数据)
|
# 开发/测试时使用 H2 内存数据库(每次重启清空数据)
|
||||||
spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
# spring.datasource.url=jdbc:h2:mem:vibevault;DB_CLOSE_DELAY=-1
|
||||||
spring.datasource.username=sa
|
# spring.datasource.username=sa
|
||||||
spring.datasource.password=
|
# spring.datasource.password=
|
||||||
spring.datasource.driver-class-name=org.h2.Driver
|
# spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
|
||||||
# 如需使用 PostgreSQL,注释上面的 H2 配置,取消下面的注释
|
# 生产环境使用 PostgreSQL 远程数据库
|
||||||
# spring.datasource.url=jdbc:postgresql://localhost:5432/vibevault
|
spring.datasource.url=jdbc:postgresql://49.234.193.192:5432/vibevault
|
||||||
# spring.datasource.username=postgres
|
spring.datasource.username=postgres
|
||||||
# spring.datasource.password=postgres
|
spring.datasource.password=postgres
|
||||||
# spring.datasource.driver-class-name=org.postgresql.Driver
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
|
|
||||||
# JPA / Hibernate
|
# JPA / Hibernate
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
|
|||||||
59
src/test/java/com/vibevault/DatabaseConnectionTest.java
Normal file
59
src/test/java/com/vibevault/DatabaseConnectionTest.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
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