簡體   English   中英

Java Spring JPA存儲庫

[英]Java Spring JPA Repository

我是Spring菜鳥,正在努力。

基本上,在開始使用Spring和JPA一起開發Server之前,我試圖開始一個簡單的示例,以適應該框架。 我已經成功使make Spring與某些框架一起使用,例如Log4J,Swagger等。 現在,我正在嘗試使用JPA,我可以從中找到一些解決方案。

我看到了一些有關如何使用它進行開發的博客,以及從我選擇創建我的Repository Interfece並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存儲庫以在此處共享代碼。

關於我在做什么錯的任何線索嗎?

只需使用@Repository注釋您的界面。 如果那不起作用,請嘗試將@EnableJPARepositories添加到主類。

這里的第一件事是為什么您需要執行基本接口存儲庫 ,而不用執行通常的CRUD操作。 對於此類操作,最好實現CrudRepository 由於您實現了CrudRepository,因此無需定義findAll()方法以及可以在文檔中找到的許多其他知名方法。

此外,當使用@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->控制器
  4. com.example-> MainApplication類

這是默認項目結構的示例。主應用程序類應位於其他應用程序的更高級別的程序包中。 除非必須使用@ComponentScan指定軟件包的位置。

正如您是該框架的初學者一樣。 我建議您始終在官方文檔中查看類定義。

更新

這是我的一個春季靴子項目的例子

在此處輸入圖片說明

另請參閱本JPA 春季指南

嘗試將@Repository批注添加到PersonRepository中,這可能是找不到它的原因。

您需要使用@Respository注釋PersonRepository接口,否則spring上下文將無法識別它或為其創建實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM