Compare commits

...

2 Commits

Author SHA1 Message Date
531de40282 完成作业:解决合并冲突,配置远程PostgreSQL数据库
Some checks failed
autograde-final-vibevault / check-trigger (push) Successful in 5s
autograde-final-vibevault / grade (push) Failing after 32s
2025-12-22 05:19:13 +08:00
e2a2a72754 完成作业 2025-12-22 05:07:33 +08:00
35 changed files with 343 additions and 103 deletions

89
DATABASE_CONNECTION.md Normal file
View 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支持
- ✅ 数据库迁移 (如果需要)

View 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

View 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

View 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

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.

188
gradlew.bat vendored
View File

@ -1,94 +1,94 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
set CLASSPATH=
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2
goto fail
:execute
@rem Setup the command line
set CLASSPATH=
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
:end
@rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View 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

View File

@ -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注释上面的 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
# 生产环境使用 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

View 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);
}
}