JavaSpringJPA存储库

JavaSpringJPA存储库,java,spring,spring-data,spring-data-jpa,Java,Spring,Spring Data,Spring Data Jpa,我是一个春天的傻瓜,我正在和它斗争 基本上,在开始使用Spring和JPA开发我的服务器之前,我试着开始一个简单的例子来适应这个框架。 我已经在makespring中成功地使用了一些框架,如Log4J、Swagger和其他框架。现在我正在尝试与JPA合作,我可以找到一些解决方案 我看到了一些关于如何使用它进行开发的博客,从我选择的所有数千个选项中,我选择了创建我的存储库interece和扩展存储库。你可以看到我的代码如下: package com.example.model; @Entity p

我是一个春天的傻瓜,我正在和它斗争

基本上,在开始使用Spring和JPA开发我的服务器之前,我试着开始一个简单的例子来适应这个框架。 我已经在makespring中成功地使用了一些框架,如Log4J、Swagger和其他框架。现在我正在尝试与JPA合作,我可以找到一些解决方案

我看到了一些关于如何使用它进行开发的博客,从我选择的所有数千个选项中,我选择了创建我的存储库interece和
扩展存储库
。你可以看到我的代码如下:

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<Person, Integer> {
    Collection<Person> findAll();
}


package com.example.controller;
@RestController
public class PersonController {

    @Autowired
    private PersonRepository repo;

    @RequestMapping(value = "/persons", method = RequestMethod.GET)
    public Collection<Person> getAll() {
        return repo.findAll();
    }
}

package com.example;
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
当我运行服务器时,出现以下异常:

: 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注释您的界面。如果这不起作用,请尝试将@EnableJPARepositories添加到主类。

只需使用@Repository注释您的接口即可。如果不起作用,请尝试将@EnableJPARepositories添加到主类。

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

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

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

此外,当使用
@SpringBootApplication
springboot时,请使用默认值。如果您看到@springboot应用程序,您将看到:

许多Spring Boot开发人员总是用@Configuration、@EnableAutoConfiguration和@ComponentScan注释他们的主类。由于这些注释经常一起使用(特别是在遵循上述最佳实践的情况下),SpringBoot提供了一个方便的@SpringBootApplication替代方案

@SpringBootApplication注释相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan及其默认属性:[……]

这意味着当使用组件的默认值时,扫描包的结构应如下所示:

  • com.example.model->您的实体
  • com.example.repository->您的存储库
  • com.example.controller->controllers
  • com.example->main应用程序类
  • 下面是一个例子 主应用程序类应该位于比其他类更高级别的包中。除非您必须使用@ComponentScan指定包位置

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

    更新

    下面是我的一个spring boot项目的示例


    对于JPA也可以看到这一点,这里的第一件事是为什么需要实现基本接口,因为这样做,您将不会有通常的CRUD操作。对于这样的操作,最好执行。由于您实现了无需定义findAll()方法和许多众所周知的其他方法,所以您可以在前面提到的文档中找到它们

    此外,当使用
    @SpringBootApplication
    springboot时,请使用默认值。如果您看到@springboot应用程序,您将看到:

    许多Spring Boot开发人员总是用@Configuration、@EnableAutoConfiguration和@ComponentScan注释他们的主类。由于这些注释经常一起使用(特别是在遵循上述最佳实践的情况下),SpringBoot提供了一个方便的@SpringBootApplication替代方案

    @SpringBootApplication注释相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan及其默认属性:[……]

    这意味着当使用组件的默认值时,扫描包的结构应如下所示:

  • com.example.model->您的实体
  • com.example.repository->您的存储库
  • com.example.controller->controllers
  • com.example->main应用程序类
  • 下面是一个例子 主应用程序类应该位于比其他类更高级别的包中。除非您必须使用@ComponentScan指定包位置

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

    更新

    下面是我的一个spring boot项目的示例


    对于JPA也可以看到这一点。

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

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

    类的包是什么?只有当您的存储库与App类位于同一个或子包中时,才会自动检测到它。@dunni我删除它只是为了简化,但它们是:package com.example.controllers;包com.example.entities;包com.example.repository;包com.example;假设您拥有所有依赖项。在你的应用程序类中,你能把它放在@SpringBootApplication:@ComponentScan(basePackages=“com.example”)下面吗。然后再试一次。@KennyTaiHuynh SpringBootApplication ComponentScan(basePackages=“com.example”)公共类应用程序{相同的例外:SI mean在@SpringBootApplication行的下面。在另一行中,放置@ComponentScan(basePackages=“com.example”)。您的类的包是什么?只有当您的存储库与App类位于同一个包或子包中时,才会自动检测到它。@dunni我删除它们只是为了简化,但它们是:package com.example.controllers;package com.example.entities;package com.example.repository;package com.example;假设您拥有所有依赖项密度。
    : 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)}