簡體   English   中英

接口的 Spring Boot 存儲庫

[英]Spring Boot Repository of an interface

是否有可能基於 Spring Boot Data 中的接口創建存儲庫? 我制作了這段代碼,其中Medicamento是許多模型實現它的接口,但沒有成功:

public interface MedicamentoRepository extends JpaRepository<Medicamento, Long> {
    public <T extends Medicamento> List<Medicamento> findItens(Class<T> type);
}

謝謝!

使用 Spring Data JPA,Spring 負責生成實現並將其注冊為 Spring 托管組件。 您所要做的就是通過在 Spring 配置類上添加@EnableJpaReositories並指定存儲庫接口所在的包來正確配置它。

CrudRepository接口(和JpaRepository )已經提供了所有基本方法,如findOnefindAlldelete 、 ... ,因此您不需要在您的接口中添加它們。

如果您需要添加特定的方法,那么 Spring 將根據您的方法和參數的名稱(例如findByName ,其中name是實體的字段)或使用@Query注釋生成實現。

為了更好地了解 Spring Data JPA 的工作原理和所有可用功能,請查看他們的文檔: https : //docs.spring.io/spring-data/jpa/docs/current/reference/html/

暫無
暫無

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

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