Java Spring JPA Repository

我是一个spring的菜鸟,我正在努力。

基本上在开始使用Spring与JPA一起开发我的服务器之前,我试图开始一个简单的例子,以便习惯这个框架。 我已经成功使Spring使用一些框架如Log4J,Swagger等。 现在我正在尝试使用JPA,我可以找到一些解决方案。

我看到了一些关于如何用它开发的博客,以及我选择创建Repository Interfece并extend Repository数千个选项。 您可以看到我的代码:

 package com.example.model; @Entity public class Person { @Id public Integer id; public String name; public Person(){} } package com.example.repository; public interface PersonRepository extends Repository { Collection findAll(); } package com.example.controller; @RestController public class PersonController { @Autowired private PersonRepository repo; @RequestMapping(value = "/persons", method = RequestMethod.GET) public Collection getAll() { return repo.findAll(); } } package com.example; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } 

我还有application.properties文件:

 spring.datasource.platform=postgres spring.datasource.url=jdbc:postgresql://localhost:5432/test_db spring.datasource.username=test spring.datasource.password=test spring.datasource.driver-class-name=org.postgresql.Driver 

当我运行服务器时,我得到以下exception:

 : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: : Closing JPA EntityManagerFactory for persistence unit 'default' : Stopping service Tomcat : Application startup failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

我创建了一个Github存储库来分享这里的代码。

关于我做错了什么的任何线索?

这里的第一件事是为什么你需要实现基本接口Repository ,这样做,你将没有通常的CRUD操作。 对于这样的操作,最好是实现CrudRepository 。 由于您实现了CrudRepository,因此无需定义findAll()方法和许多众所周知的其他方法,您可以在上面提到的doc中找到它们。

此外,使用@SpringBootApplication Spring引导使用默认值。 如果您看到@SpringBootApplication 定义,您将看到:

许多Spring Boot开发人员总是使用@Configuration,@ EnableAutoConfiguration和@ComponentScan注释其主类。 由于这些注释经常一起使用(特别是如果您遵循上面的最佳实践),Spring Boot提供了一个方便的@SpringBootApplication替代方案。

@SpringBootApplication注释等同于使用@Configuration,@ EnableAutoConfiguration和@ComponentScan及其默认属性:[…]

这意味着当使用ComponentScan的默认值时,您的软件包结构如下:

  1. com.example.model – >您的实体
  2. com.example.repositoriy – >您的存储库
  3. com.example.controller – > controllers
  4. com.example – > MainApplication类

下面是默认项目结构的示例主应用程序类应该在更高级别的包中,然后是其他类。 除非您必须使用@ComponentScan指定包位置。

因为你是框架的初学者。 我建议你总是在官方文档中看到类定义。

更新

这是我的一个春季启动项目的一个例子

在此处输入图像描述

另请参阅JPA 春季指南

只需使用@Repository注释您的界面。 如果这不起作用,请尝试将@EnableJPARepositories添加到主类中。

尝试将@Repository注释添加到PersonRepository,这可能是它找不到它的原因。

您需要使用@Respository注释PersonRepository接口,否则spring上下文将无法识别它或为其创建实现。