简体   繁体   English

H2 数据库是用 spring-boot-starter-data-jpa 创建的,但不是用 spring-boot-starter-data-jdbc 创建的

[英]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-jpaspring-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.

相关问题 Spring 引导不添加 spring-boot-starter-data-jpa - Spring boot not adding spring-boot-starter-data-jpa Java:依赖项目中的spring-boot-starter-data-jpa依赖 - Java : spring-boot-starter-data-jpa dependency in a dependent project spring-boot-starter-data-jpa依赖错误 - spring-boot-starter-data-jpa dependency error 有关Spring-boot-starter-data-jpa的问题 - A question about the Spring-boot-starter-data-jpa spring-boot-starter-data-jpa:自动重新连接 - spring-boot-starter-data-jpa : Auto Reconnect 在单元测试中带有spring-boot-starter-data-jpa的Spring Boot需要强制使用@DataJpaTest - Spring Boot with spring-boot-starter-data-jpa in unit test needs mandatory @DataJpaTest spring-data-jpa和spring-boot-starter-data-jpa之间的区别 - Difference between spring-data-jpa and spring-boot-starter-data-jpa 未找到依赖项 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3' - Dependency 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3' not found 将“spring-boot-starter-data-jpa”依赖项添加到Spring项目时出错 - Error when adding “spring-boot-starter-data-jpa” dependency to Spring project 排除 JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' 启动时导致 CRUDRepository 错误 - Exclude JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' causes error in CRUDRepository when starting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM