簡體   English   中英

Spring boot 2.0如何實現CrudRepository的自定義方法?

[英]How to implement custom method of CrudRepository in Spring boot 2.0?

我發現這個問題之前已經被問過。 但是答案和文檔都不適合我。 我關注這個文檔。

我想向存儲庫添加 2 個自定義界面。 我一定是在我的代碼中做錯了什么。

編譯器報錯:

Error:(18, 8) java: com.ma.home.service.BuyerPartyDetailsServiceImpl 不是抽象的並且沒有覆蓋 org.springframework.data.repository.CrudRepository 中的抽象方法 deleteAll()。

自定義存儲庫:

  @Repository
  public interface CustomBuyerPartyDeatilsDAO {
    @Query("SELECT b FROM BuyerPartyDetails buyer WHERE LOWER(buyer.xmlFileName) = fileName")
    public List<BuyerPartyDetails> listByfileName(String fileName);

    @Query("SELECT b FROM BuyerPartyDetails buyer WHERE LOWER(buyer.buyerOrganisationName) = name")
    public BuyerPartyDetails getByBuyerPartyByName(String name);

}

存儲庫:

  @Repository
  public interface BuyerPartyDetailsDAO2 extends 
CrudRepository<BuyerPartyDetails, Long>, CustomBuyerPartyDeatilsDAO {

    public List<BuyerPartyDetails> listByfileName(String fileName);
    public BuyerPartyDetails getByBuyerPartyByName(String name);
}

服務:

   @Service
   public class BuyerPartyDetailsServiceImpl implements 
 BuyerPartyDetailsDAO2, 
  CustomBuyerPartyDeatilsDAO {


    @Autowired
    private BuyerPartyDetailsDAO2 buyerPartyDetailsDAO2;

    public BuyerPartyDetailsServiceImpl() {

    }

  // more implementation removed.  
 .......
 .......
 }

如果我提供deleteAll()的實現,它會一一詢問所有CrudRepository方法,例如save()saveAll()findById()findAll()等。

那么如何使用CrudRepository已經實現的方法呢?

要解決此錯誤

com.ma.home.service.BuyerPartyDetailsServiceImpl 不是抽象的,不會覆蓋 org.springframework.data.repository.CrudRepository 中的抽象方法 deleteAll()

您必須覆蓋所有抽象方法並為它們提供實現以使其發揮作用。

如果您不需要全部實現它們,那么您必須將 DAO 接口注入服務並讓服務實現它們自己的接口。

Spring 將通過提供您應該在應用程序上下文中配置的接口的合適實現來完成剩下的工作。

暫無
暫無

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

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