2015-12-21 65 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方法。

相关问题