SpringData系列三:Repository接口

 Repository 接口是 Spring Data 的一个核心接口,是一个空接口,即是一个标记接口。它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法。

public interface Repository<T, ID extends Serializable> {
} 

 若我们定义的接口继承了 Repository, 则该接口会被 IOC 容器识别为一个 Repository Bean。纳入到 IOC 容器中. 进而可以在该接口中定义满足一定规范的方法。

public interface PersonRepsotory extends Repository<Person, Integer>{
}

 与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。

@RepositoryDefinition(domainClass=Person.class,idClass=Integer.class)
public interface PersonRepsotory {
}

 基础的 Repository 提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下:
在这里插入图片描述
 Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类。
 CrudRepository: 继承 Repository,实现了一组 CRUD 相关的方法。
 PagingAndSortingRepository: 继承 CrudRepository,实现了一组分页排序相关的方法。
 JpaRepository: 继承 PagingAndSortingRepository,实现一组 JPA 规范相关的方法 。
 自定义的 Repository: 需要继承 JpaRepository,这样的 自定义的Repository 接口就具备了通用的数据访问控制层的能力。
 JpaSpecificationExecutor: 不属于Repository体系,实现一组 JPA Criteria 查询相关的方法。

猜你喜欢

转载自blog.csdn.net/lizhiqiang1217/article/details/89786274