Spring H2 数据库 EmbeddedDatabaseFactory 错误连接 URL [英] Spring H2 database EmbeddedDatabaseFactory wrong connection URL
问题描述
我正在尝试使用 Spring-Boot 2、Java 11、H2 数据库和 Gradle 构建一个简单的 REST 后端.我想使用嵌入式(文件)H2 数据库,所以我的(相关)application.yml
(我知道它被正确读取,因为其他值有效)看起来像这样:
I'm trying to build a simple REST-Backend with Spring-Boot 2, Java 11, H2 database and Gradle. I want to use an embedded (file) H2 database, so my (relevant) application.yml
(I know it is read in correctly because other values work) looks like this:
spring:
datasource:
url: "jdbc:h2:./customdb"
username: sa
driver-class-name: org.h2.Driver
jpa:
database-platform: org.hibernate.dialect.H2Dialect
show-sql: false
hibernate:
ddl-auto: update
我的build.gradle
:
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'net.impfox'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile 'org.springframework.boot:spring-boot-starter-data-rest:2.1.3.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
compile 'javax.persistence:javax.persistence-api:2.2'
compile 'org.hibernate:hibernate-core'
compile 'org.springframework.data:spring-data-jpa'
}
但是,当我启动应用程序时,没有创建文件并且日志显示:
However, when I start the application, there is no file created and the log says:
...EmbeddedDatabaseFactory: Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
我不太清楚它为什么说Starting embedding database
,因为正如您在url 中看到的那样,它实际上启动了一个名为testdb
的内存数据库.那么为什么它使用 jdbc:h2:mem:testdb
作为 JDBC url 而不是我配置的 jdbc:h2:./customdb
?
I'm not quite sure why it says Starting embedded database
because as you can see in the url, it actually starts an in-memory database called testdb
.
So why is it using jdbc:h2:mem:testdb
as JDBC url and not my configured jdbc:h2:./customdb
?
推荐答案
检查您的 gradle 依赖项!
Check your gradle dependencies!
代替编译'org.springframework.data:spring-data-jpa'
,
使用 spring-boot-starter-data-jpa,所以类似编译'org.springframework.boot:spring-boot-starter-data-jpa:2.1.4.RELEASE'
.
还要确保 gradle 实际上正在应用此更改.
Instead of
compile 'org.springframework.data:spring-data-jpa'
,
use the spring-boot-starter-data-jpa, so something like
compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.1.4.RELEASE'
.
Also make sure gradle is actually applying this change.
问题是 spring-data-jpa 不包括所有需要的功能.spring-boot-starter-data-jpa 依赖于 spring-data-jpa,但也依赖于其他一些工件.
The problem is that spring-data-jpa doesn't include all the functionality needed. The spring-boot-starter-data-jpa depends on the spring-data-jpa, but also on some other artifacts.
这篇关于Spring H2 数据库 EmbeddedDatabaseFactory 错误连接 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!