Spring Data 之 Repository 接口

1. 介绍

  • Repository是一个空接口,即是一个标记性接口;
  • 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个 Repository Bean;
  • 也可以通过@RepositoryDefinition注解,来替代继承Repository接口;
// 源码:
public interface Repository<T, ID extends Seriallizable>{

}

// 使用继承的方式
public interface PersonRepository extends Repository<Person, Integer>{
    Person getByLastName(String lastName);
}

// 使用注解替代继承
@RepositoryDefinition(domainClass=Person.class, idClass=Integer.class)
public interface PersonRepository{
    Person getByLastName(String lastName);
}

2. Repository接口的子接口

  • CrudRepository:继承Repository,实现了一组CRUD相关的方法;
  • PagingAndSortingRepository:继承CrudRepository,实现了一组分页排序相关的方法;
  • JpaRepository:继承PagingAndSotringRepository,实现一组JPA规范相关的方法;
  • 自定义的XxxRepository:可以继承JpaRepository;
  • JpaSpecificationExecutor:不属于Repository体系,实现一组 JPA Criteria 查询相关的方法;

参考资料:

猜你喜欢

转载自www.cnblogs.com/linkworld/p/9164331.html