Java Spring JPA 存储库

标签 java spring spring-data spring-data-jpa

我是一个 Spring 菜鸟,我正在为此苦苦挣扎。

基本上,在开始使用 Spring 与 JPA 结合开发我的服务器之前,我尝试启动一个简单的示例,只是为了习惯这个框架。 我已经成功地让 Spring 与 Log4J、Swagger 等框架一起工作。现在我正在尝试使用 JPA,有一些点我可以找到解决方案。

我看到了一些关于如何使用它进行开发的博客,并从数千个选项中我选择创建我的存储库接口(interface)和 extend Repository<T, ID> 。您可以在下面看到我的代码:

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);
    }
}

我还有 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 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 存储库来共享代码 here .

知道我做错了什么吗?

最佳答案

这里首先是为什么你需要实现基本接口(interface) Repository这样做,您将不会进行通常的 CRUD 操作。对于此类操作,最好实现 CrudRepository 。由于您实现了 CrudRepository无需定义 findAll() 方法,您可以在提到的文档中找到许多众所周知的其他方法。

此外,当使用@SpringBootApplication时,Spring boot使用默认值。如果您看到@SpringBootApplication definition你会看到:

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]

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

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

这是 default project structure 的示例 主应用程序类应该位于比其他应用程序类更高级别的包中。除非您必须使用@ComponentScan指定包位置。

由于您是该框架的初学者。我建议您始终在官方文档中查看类定义。

更新:

这是我的一个 Spring Boot 项目的示例

enter image description here

另请参阅此 spring guide对于 JPA

关于Java Spring JPA 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36944587/

相关文章:

java - JAXB 编码 NumberFormatException 不是数字 : 2. 44​​4 at com.sun.xml.bind.DatatypeConverterImpl._parseInt(DatatypeConverterImpl.java:132)

java - Big Decimal 总是四舍五入到两位小数(即使我的 BigDecimal 对象没有小数)?

java - Spring MVC 调度程序 servlet.xml 错误

java - Spring 数据休息: Tracking data changes in history tables

java - 使用 JPA 规范过滤优化聚合值选择

Neo4j:不知道如何将图形映射到 Spring Data bean

java - Android - 抽屉导航实现 Activity 更改

java - NumberFormatException:无法将 '' 解析为整数

java - 使用 Junit 测试 Spring Singleton 实现

java - Spring MVC中无法访问maven项目资源