问题描述01
jpa分页查询,error:Page 1 of 0 containing UNKNOWN instances, 查出来的数据库数据与javabean不匹配
/**
* page = 1
* size = 10
* isAsc = true
*/
PageRequest.of(page, size, isAsc ? Sort.Direction.ASC : Sort.Direction.DESC, orderField)
原因分析01:
提示:这个问题一般是你分页查询,page为1(page默认从0开始),但没有1页(也就是没有第二页),所以你应该page从0开始赋值;
解决方案01:
public Pageable getDefaultPageable(Integer page, Integer size, Boolean isAsc, String orderField) {
page--;
if (page < 0) {
page = 0;
}
if (size < 0) {
size = 0;
}
return PageRequest.of(page, size, isAsc ? Sort.Direction.ASC : Sort.Direction.DESC, orderField);
}
// 或者:page-1即可
PageRequest.of(--page, size, isAsc ? Sort.Direction.ASC : Sort.Direction.DESC, orderField)