在我的 Spring Boot Gradle-Groovy 项目上添加依赖项但无法连接 H2 数据库

问题描述 投票:0回答:1

我尝试在 Spring Boot Gradle Groovy 项目上运行 H2 数据库并添加了所有依赖项,但数据库未运行。当我输入数据库 URL 时,它会显示一个 Whitelabel 错误页面。

这是我的build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.2'
    id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.packt'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(22)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'com.h2database:h2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

应用程序.属性

spring.application.name=cardatabase
server.port=8081
spring.datasource.url=jdbc:h2:mem:accounttestdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

```[Here is the result showing](https://i.sstatic.net/Mw70tzpB.png)
spring-boot gradle groovy spring-data-jpa
1个回答
0
投票

您需要使用

spring-starter-jpa
来启用Spring对
h2
的自动配置。您的 pom.xml
dependencies
应该如下所示:

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  implementation 'org.springframework.boot:spring-boot-starter-web'
  developmentOnly 'org.springframework.boot:spring-boot-devtools'
  runtimeOnly 'com.h2database:h2'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
© www.soinside.com 2019 - 2024. All rights reserved.