[英]H2 Database created with spring-boot-starter-data-jpa but not with spring-boot-starter-data-jdbc
I am new in Spring.我是春天的新人。 I am trying to work with H2 Database and JDBC (not JPA/Hibernate).我正在尝试使用 H2 数据库和 JDBC(不是 JPA/Hibernate)。
So with Spring Initializr, when i add theses dependencies :所以使用 Spring Initializr,当我添加这些依赖项时:
spring-boot-starter-web
spring-boot-starter-data-jpa
h2
and putting this property to true : spring.h2.console.enabled=true
并将此属性设置为 true : spring.h2.console.enabled=true
The database mem:testdb
is created and i can connect to it at : localhost:8080/h2-console
数据库mem:testdb
已创建,我可以在以下位置连接到它: localhost:8080/h2-console
But when i change the jpa dependency to :但是当我将 jpa 依赖项更改为:
spring-boot-starter-data-jdbc
The database is not created and I have this error message :未创建数据库,我收到此错误消息:
Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149 (Help)
So I would like to know why it doesn't work with JDBC, and if you know a solution to make it work.所以我想知道为什么它不能与 JDBC 一起使用,以及您是否知道使其工作的解决方案。
I found two articles on the web, and it seems they can have the database created with JDBC :我在网上找到了两篇文章,似乎他们可以使用 JDBC 创建数据库:
UPDATE :更新 :
I tried to do the same thing at home in my personal computer and it works...我试图在家里的个人电脑上做同样的事情,它有效......
I don't know why it doesn't work in job computer although it works with JPA.我不知道为什么它在工作计算机中不起作用,尽管它适用于 JPA。
In the logs, it is missing these lines :在日志中,它缺少以下几行:
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
osbah2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
UPDATE2 :更新2:
I found the solution :我找到了解决方案:
As I said, I am using Spring Initializr to bootstrap the project.正如我所说,我使用 Spring Initializr 来引导项目。 In the job pc, I was not able to use the last version of Spring boot (2.2.4), so i choosed 2.1.12在job pc中,无法使用最新版本的Spring boot(2.2.4),所以我选择了2.1.12
Version Spring Boot版本 Spring Boot
With this version, it works with JPA but not with JDBC...在这个版本中,它适用于 JPA 但不适用于 JDBC ...
From the documentation从文档
You need a dependency on spring-jdbc for an embedded database to be auto-configured.您需要依赖 spring-jdbc 才能自动配置嵌入式数据库。
spring-boot-starter-data-jpa and spring-boot-starter-data-jdbc gets spring-jdbc transitively through its dependency on spring-boot-starter-jdbc spring-boot-starter-data-jpa和spring-boot-starter-data-jdbc通过对spring-boot-starter-jdbc 的依赖来传递性地获取 spring -jdbc
This means for both these dependencies h2 will be auto-configured.这意味着对于这两个依赖项 h2 将被自动配置。
Update:更新:
For both configurations I am able to access the db with http://localhost:8080/h2-console/对于这两种配置,我都可以使用http://localhost:8080/h2-console/访问数据库
and you will be getting a similar log when spring boot application starts up当 spring boot 应用程序启动时,你会得到一个类似的日志
2020-02-05 01:27:16.135[0;39m [32m INFO[0;39m [35m55966[0;39m [2m---[0;39m [2m[ main][0;39m [36mo.s.b.a.h2.H2ConsoleAutoConfiguration [0;39m [2m:[0;39m H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
Both links provided by you allow me to create spring boot application with the following dependencies: Web, JDBC, H2, DevTools.您提供的两个链接都允许我创建具有以下依赖项的 Spring Boot 应用程序:Web、JDBC、H2、DevTools。 If change 'spring-boot-starter-jdbc' dependency to 'spring-boot-starter-data-jpa' everything works the same way, eg H2 database is created and accessible.如果将 'spring-boot-starter-jdbc' 依赖项更改为 'spring-boot-starter-data-jpa' 一切都以相同的方式工作,例如创建和访问 H2 数据库。 Make sure you specified the same configuration in both cases in the application.properties file, 'spring.datasource.url' hasn't been changed.确保在 application.properties 文件中在两种情况下指定了相同的配置,'spring.datasource.url' 没有改变。
I found the solution :我找到了解决方案:
As I said, I am using Spring Initializr to bootstrap the project.正如我所说,我使用 Spring Initializr 来引导项目。 In the job pc, I was not able to use the last version of Spring boot (2.2.4), so i choosed 2.1.12在job pc中,无法使用最新版本的Spring boot(2.2.4),所以我选择了2.1.12
Version Spring Boot版本 Spring Boot
With this version, it works with JPA but not with JDBC...在这个版本中,它适用于 JPA 但不适用于 JDBC ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.