这篇文章主要讲解了“Spring JPA find分页方法怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring JPA find分页方法怎么使用”吧!
findAll(Pageable pageable):从入参 pageable 我们可以猜到,就是这个对象来帮助我们对数据进行分页查询。那我们如何创建 pageable 对象呢?如何用他去实现分页呢?先看一下源码:
/** * Creates a new {@link Pageable} for the first page (page number {@code 0}) given {@code pageSize} . * * @param pageSize the size of the page to be returned, must be greater than 0. * @return a new {@link Pageable}. * @since 2.5 */ static Pageable ofSize(int pageSize) { return PageRequest.of(0, pageSize); }
由此可见,pageable 对象需要 pageRequest.of 去创建,那我们再看看 pageRequest.of 的源码:
/** * Creates a new unsorted {@link PageRequest}. * * @param page zero-based page index, must not be negative. * @param size the size of the page to be returned, must be greater than 0. * @since 2.0 */ public static PageRequest of(int page, int size) { return of(page, size, Sort.unsorted()); } /** * Creates a new {@link PageRequest} with sort parameters applied. * * @param page zero-based page index. * @param size the size of the page to be returned. * @param sort must not be {@literal null}, use {@link Sort#unsorted()} instead. * @since 2.0 */ public static PageRequest of(int page, int size, Sort sort) { return new PageRequest(page, size, sort); } /** * Creates a new {@link PageRequest} with sort direction and properties applied. * * @param page zero-based page index, must not be negative. * @param size the size of the page to be returned, must be greater than 0. * @param direction must not be {@literal null}. * @param properties must not be {@literal null}. * @since 2.0 */ public static PageRequest of(int page, int size, Direction direction, String... properties) { return of(page, size, Sort.by(direction, properties)); }
从源码可以看出,pageRequest.of 需要传入 page(第几页)和 size(页面大小),另外如果需要排序的话,还需要传入 sort。
所谓的单纯分页查询,就是只分页,不做其他的动作,这样一来,我们只需要传入 page 和 pageSize 即可。
control 层
@GetMapping("findAllPage") public Page<User> findAllPage(int page, int pageSize) { Pageable pageable = PageRequest.of(page, pageSize); return userService.findAllPage(pageable); }
执行请求findAllPage?page=0&pageSize=5
(第一页、页面大小为5),控制台打印如下:
Hibernate: select user0\_.id as id1\_0\_, user0\_.age as age2\_0\_, user0\_.name as name3\_0\_ from user user0\_ limit ? Hibernate: select count(user0\_.id) as col\_0\_0\_ from user user0\_
{ "content": [{ "id": 20, "name": "aa", "age": 11 }, { "id": 21, "name": "bb", "age": 12 }, { "id": 22, "name": "cc", "age": 11 }, { "id": 23, "name": "dd", "age": 16 }, { "id": 24, "name": "ee", "age": 17 } ], "pageable": { "sort": { "empty": true, "sorted": false, "unsorted": true }, "offset": 0, "pageSize": 5, "pageNumber": 0, "unpaged": false, "paged": true }, "last": false, "totalPages": 2, "totalElements": 6, "number": 0, "size": 5, "sort": { "empty": true, "sorted": false, "unsorted": true }, "numberOfElements": 5, "first": true, "empty": false }
从上面的结果输出和控制台输出来看,它除了做了 limit 分页查询外,还做了求出了总数totalElements,还输出了总页数 totalPages。这些参数在我们实际项目中有的时候还是很有用的。
所谓的排序分页,就是将数据先按照我们所需的方式进行排序,然后再进行分页查询
control 层
@GetMapping("findAllPageSort") public Page<User> findAllPageSort(int page, int pageSize, String[] sorts, String[] paras) { List<Sort.Order> listOrder = new ArrayList<>(); for(int i=0; i<sorts.length; i++){ listOrder.add(new Sort.Order(sorts[i].toLowerCase().equals("asc") ? Sort.Direction.ASC : Sort.Direction.DESC, paras[i])); } Pageable pageable = PageRequest.of(page, pageSize, Sort.by(listOrder)); return userService.findAllPage(pageable); }
执行请求findAllPageSort?page=0&pageSize=5&sorts=asc,desc¶s=age,name
(第一页,页面大小为5,先按照age升序,再按照name降序),控制台打印如下:
Hibernate: select user0_.id as id1_0_, user0_.age as age2_0_, user0_.name as name3_0_ from user user0_ order by user0_.age asc, user0_.name desc limit ?
Hibernate: select count(user0_.id) as col_0_0_ from user user0_
{ "content": [{ "id": 26, "name": "gg", "age": 7 }, { "id": 22, "name": "cc", "age": 11 }, { "id": 20, "name": "aa", "age": 11 }, { "id": 21, "name": "bb", "age": 12 }, { "id": 23, "name": "dd", "age": 16 } ], "pageable": { "sort": { "empty": false, "sorted": true, "unsorted": false }, "offset": 0, "pageNumber": 0, "pageSize": 5, "paged": true, "unpaged": false }, "totalElements": 7, "last": false, "totalPages": 2, "number": 0, "size": 5, "sort": { "empty": false, "sorted": true, "unsorted": false }, "numberOfElements": 5, "first": true, "empty": false }
以下是整理的 pageable 对象可用的一些比较实用的方法
方法 | 说明 |
---|---|
boolean unpaged | true:未进行分页,false:进行了分页 |
boolean isPaged | true:进行了分页,false:未进行分页 |
int getPageNumber | 获取当前页数 |
int getPageSize | 获取页面大小 |
long getOffSet | 获取页面偏移量,相当于sql中的start |
Sort getSort | 获取当前的排序规则 |
Pageable next | 获取下一页的分页信息(翻页中的下一页) |
boolean hasPrevious | 是否有上一页 |
Pageable previous | 获取上一页的分页信息(翻页钟的上一页) |
Pageable first | 获取首页分页信息 |
Pageable previousOrFirst | 如果没有上一页则回到首页 |
感谢各位的阅读,以上就是“Spring JPA find分页方法怎么使用”的内容了,经过本文的学习后,相信大家对Spring JPA find分页方法怎么使用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。