JPA#Interfaces总结
_开局一张图,内容全靠编
震惊:某小白熟练使用了JpaRepository和JpaSpecificationExecutor,就在简历上写下了,精通SpringData Jpa。
震惊,如果想熟练的使用SpringData JPA 对数据库进行操作,只需要重点关注上图中框住的5个接口,和其他的一些相关接口。
接口名称 | 要点 |
Repository | 1.方法命名查询。2.Query注解查询的支持。 |
CrudRepository | 1.继承Repository。2.Crud操作。 |
PagingAndSortingRepository | 1.继承了CrudRepository。2.能够分页和排序。3.无法进行复杂的查询。 |
JpaRepository | 1.继承了PagingAndSortingRepository。2.对返回值的类型进行了统一。3.通常与JpaSpecificationExecutor配合使用。 |
JpaSpecificationExecutor | 1.复杂查询的支持。2.分页排序的支持。3.通常与JpaRepository配合使用。 |
---
--
其他但是同样重要的。
JpaSpecificationExecutor提供的方法如下:
涉及到了两个重要的接口,分别表示查询的where已经查询的分页信息。其中分页信息中又包含了排序信息。
org.springframework.data.domain.Pageable接口用于表示分页信息,通常用其实现类org.springframework.data.domain.PageRequest
//code--begin
// PageRequest的构造又会涉及到Sort及其内部类Direction和Order // org.springframework.data.domain.Sort // org.springframework.data.domain.Sort.Direction // org.springframework.data.domain.Sort.Order //--------------------------------------------- //排序的构造方法及其使用示列 //--------------------------------------------- /** * Creates a new Sort instance using the given Orders. * * @param orders must not be null. */ public Sort(Order... orders); /*使用示列*/ // new Sort(new Order(Direction.DESC, "id")); /** * Creates a new Sort instance. * * @param orders must not be null or contain null. */ public Sort(List<Order> orders); /*使用示列*/ // new Sort(Arrays.asList(new Order(Direction.DESC,"name"),new Order(Direction.ASC,"age"))); /** * Creates a new Sort instance. Order defaults to Direction#ASC. * * @param properties must not be null or contain null or empty strings */ public Sort(String... properties); /*使用示列*/ // new Sort("name","age"); /** * Creates a new Sort instance. * * @param direction defaults to Sort#DEFAULT_DIRECTION (for null cases, too) * @param properties must not be null, empty or contain null or empty strings. */ public Sort(Direction direction, String... properties); /*使用示列*/ // new Sort(Direction.DESC,"name","age"); /** * Creates a new Sort} instance. * * @param direction defaults to Sort#DEFAULT_DIRECTION (for null cases, too) * @param properties must not be null or contain null or empty strings. */ public Sort(Direction direction, List<String> properties); /*使用示列*/ // new Sort(Direction.DESC, Arrays.asList("name","age")); //--------------------------------------------- // PageRequest的构造方法 //--------------------------------------------- /** * Creates a new PageRequest. Pages are zero indexed, thus providing 0 for page will return the first * page. * * @param page zero-based page index. * @param size the size of the page to be returned. */ public PageRequest(int page, int size) { this(page, size, null); } /** * Creates a new PageRequest with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param direction the direction of the Sort to be specified, can be null. * @param properties the properties to sort by, must not be null or empty. */ public PageRequest(int page, int size, Direction direction, String... properties) { this(page, size, new Sort(direction, properties)); } /** * Creates a new PageRequest with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param sort can be null. */ public PageRequest(int page, int size, Sort sort) { super(page, size); this.sort = sort; }
//code--end
_
_