如何从 Spring 和 Hibernate 开始

How to start with Spring and Hibernate

我要做的很简单Spring + 基于Vaadin框架的Hibernate应用。 我是 java 的新手。我看了几十个小时的教程,并阅读了我找到的所有内容。但是我找不到答案。

我使用 spring initializr 创建我的应用程序的基础(使用 JPA、Vaadin、JDBC)检查。如果我发表评论,我发现了如何启动该应用程序:

SpringApplication.run(RentalApplication.class, args);

但我要找的不是这种情况,我想使用Spring。 我坚持:

'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed;

我找到了web.xml配置的教程,但是基于Vaadin项目没有web.xml。

大部分教程已经过时,或者假设您已经对 Hibernate 和 Spring 框架了解很多。

为了完成这个答案,当不使用嵌入式数据库如H2时,为了Spring Boot与JDBC和JPA一起工作,它还需要在Spring Boot中进行设置配置文件:application.properties(如果使用 yml,则为 application.yml)

例如 PostgreSQL:

spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

可能您在网上找到的教程使用 H2 数据库,因此它们不包含这些设置。

无论如何,当然你还需要依赖 JDBC 驱动程序(例如 pom.xml)

顺便提一下,这里有一个link官方文档,里面有详细的解释:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-configure-datasource

以及关于嵌入式数据库的具体部分:

Spring Boot can auto-configure embedded H2, HSQL and Derby databases. You don’t need to provide any connection URLs, simply include a build dependency to the embedded database that you want to use.