2015-12-21 67 views
-2

is said here,即要將一個單一方法添加到存儲庫,需要創建3(三個)類或接口。在Spring JPA中擴展接口以創建存儲庫的想法是什麼?

這是真的,這樣的曼波 - 緬博的目的是什麼?接口擴展的唯一好處是能否通過命名約定來創建方法?這個好處是否真的超過了創造自己方法的能力的喪失?

UPDATE

爲什麼我不能做implements CrudRepository

我試圖用implements

// does not work 
//public abstract class CustomerRepository implements CrudRepository<Customer, Long> { 
// 
// abstract List<Customer> findByLastName(String lastName); 
//} 

// works 
public interface CustomerRepository extends CrudRepository<Customer, Long> { 

    List<Customer> findByLastName(String lastName); 
} 

,但它不與錯誤工作

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo' defined in hello.Application: Unsatisfied dependency expressed through constructor argument with index 0 of type 
[hello.CustomerRepository]: : No qualifying bean of type [hello.CustomerRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.CustomerRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
+0

這是一個凌亂的問題。你的代表表示你應該知道更多;)如何去除所有更新 - 因爲你知道這是一個太寬泛的問題,我們需要解決?然後,我們可以去解釋spring數據jpa,它基本上是在運行時自動生成實現的。 –

+0

我對Spring比較陌生。每次我閱讀他們的dox,我都有強烈的曼波 - 雅波姆感覺:) – Dims

+0

春天是做任何事情的最簡單的方法。如果你的胃部不舒服,試着閱讀JEE文檔(提示 - 他們完全是錯誤的答案;)。 –

回答

2

如果只想CRUD和分頁操作,春天已經提供了實現這些操作,雖然他們是接口。你不必實現任何這些。你只需要擴展crud和如果需要的分頁界面。 Spring將掃描存儲庫接口,並提供實現crud,分頁操作,帶查詢註釋的註釋接口方法以及如果遵守Spring數據(如findByLastname())給出的規則的方法。

有時會出現需要自行實施的情況。在這種情況下,您需要按照文檔來進行自己的實施。

Spring數據搜索擴展Repository接口的接口。而不是抽象類。

鏈接:從該鏈接

在前面的例子http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.create-instances

文本,春天指示掃描接口擴展存儲庫或一個com.acme.repositories及其所有子包其子接口。對於找到的每個接口,基礎設施註冊持久性技術特定的FactoryBean以創建處理調用查詢方法的適當代理。每個bean都是在從接口名稱派生的bean名稱下注冊的,因此UserRepository的接口將註冊在userRepository下。 base-package屬性允許使用通配符,以便您可以定義掃描包的模式。

的Javadoc庫接口:

org.springframework.data.repository.Repository
中央存儲庫標記接口。捕獲要管理的域類型以及域類型的ID類型。一般的目的是保存類型信息,並能夠在類路徑掃描期間發現擴展這個類的接口,以便輕鬆創建Spring bean。

擴展此接口的域存儲庫可以通過簡單地聲明與在CrudRepository中聲明的相同簽名的方法來選擇性地公開CRUD方法。

相關問題