spring data jpa 分页页码起始为0的问题
RTPage接口的getNumber方法,索引是0开始的,但这样的可读性不好,每次显示页码还要加1,我想要以1开始(如/manage/page/1,这样取第一页)。@Re...
RT Page接口的getNumber方法,索引是0开始的,但这样的可读性不好,每次显示页码还要加1,我想要以1开始(如/manage/page/1,这样取第一页)。
@RequestMapping("list")
public String list(@RequestParam(name="type", defaultValue="0")Integer type,
String sort, String keyword, Pageable pageable, Model model){
Page<PageViewDTO> list = pageviewService.list(type, keyword, sort, pageable);
model.addAttribute("pages", list);
return "list";
}
请问各位大神,是不是需要自己封装一个类来实现呢?还是有更简便的方法吗? 展开
@RequestMapping("list")
public String list(@RequestParam(name="type", defaultValue="0")Integer type,
String sort, String keyword, Pageable pageable, Model model){
Page<PageViewDTO> list = pageviewService.list(type, keyword, sort, pageable);
model.addAttribute("pages", list);
return "list";
}
请问各位大神,是不是需要自己封装一个类来实现呢?还是有更简便的方法吗? 展开
2个回答
展开全部
返回时,重写getNumber父类加1,接受参数时-1
public class PageNumber<T> extends PageImpl<T> implements Page<T>{
/**
* 前台传参1为起始页修改为spring data jpa的0为起始页
* @param pageable
* @return
*/
public static Pageable pageRequest(Pageable pageable){
return new PageRequest(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
}
/**
* Constructor of {@code PageImpl}.
*
* @param content the content of this page, must not be {@literal null}.
* @param pageable the paging information, can be {@literal null}.
* @param total the total amount of items available. The total might be adapted considering the length of the content
*/
public PageNumber(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
/**
* 重写当前页,将当前页加1返回前台,spring data jpa起始页0加1后返回前台
* @return
*/
@Override
public int getNumber() {
return super.getNumber()+1;
}
}
public Page<PermissionEntity> findAll(Pageable pageable) {
Page<PermissionEntity> permissionEntityPage = permissionEntityRepository.findAll(pageable);
return new PageNumber<>(permissionEntityPage.getContent(),pageable,permissionEntityPage.getTotalElements());
}
@GetMapping("/permission")
@ResponseBody
public Page<PermissionEntity> permission(@PageableDefault Pageable pageable){
return permissionEntityService.findAll(PageNumber.pageRequest(pageable));
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询