<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
src/main/resources/schema.sql
spring.sql.init.mode=always
spring.datasource.url=jdbc:h2:file:/tmp/vuls
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=none
我已经了解了SO,并且H2数据库创建的正确过程显然随着每个Spring Boot版本的变化而变化,因为有各种各样的答案不适合我。
您似乎在使用 Spring Boot 应用程序中的
schema.sql
文件初始化 H2 数据库时遇到了问题。以下是一些需要检查的事项:
application.properties
或 application.yml
文件包含指定 SQL 脚本和其他数据库配置详细信息的正确属性。使用H2嵌入:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=admin
spring.datasource.password=password
spring.sql.init.mode=embedded
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
然后访问h2控制台:
http://localhost:8080/h2-console
检查日志:在应用程序日志中查找与数据库初始化相关的任何错误消息或警告。这可以提供有关模式初始化未按预期发生的原因的宝贵见解。注意与数据库连接或架构初始化相关的任何异常或警告。
依赖项:仔细检查您的项目依赖项是否已正确配置。由于您使用 H2 作为嵌入式数据库,请确保 H2 依赖项包含在项目的构建配置中(对于 Maven 为
pom.xml
,对于 Gradle 为 build.gradle
)。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
如果您继续遇到问题,提供具体的错误消息或日志可以帮助进一步诊断问题。