Pagehelper 使用笔记


参考资料

  • pageInfo的参数说明:参考链接

  • 为什么 PageInfo.navigatepageNums 的值总为 1:参考链接

  • pagehelper官网

参数说明

PageInfo{
    pageNum=1, 				//当前页码
    pageSize=2, 			//每页显示数量
    size=2, 				//当前页显示的数量
    startRow=1, 			//开始行
    endRow=2, 				//结束行
    total=6, 				//总记录数
    pages=3, 				//总页数
    list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=6, pages=3, reasonable=false, pageSizeZero=false}, 
    prePage=0, 				//上一页
    nextPage=2, 			//下一页
    isFirstPage=true, 		//是第一页
    isLastPage=false,		// 是最后一页
    hasPreviousPage=false, 	//有上一页
    hasNextPage=true, 		//有下一页
    navigatePages=2, 		//导航页数
    navigateFirstPage=1, 	//导航到第一页
    navigateLastPage=2,  	//导航到最后一页
    navigatepageNums=[1, 2]	//导航 页数 页码
}	

使用步骤

引入依赖

        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        
        
            com.github.pagehelper
            pagehelper-spring-boot-autoconfigure
            1.4.1
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.4.1
        

配置文件

#分页pageHelper
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true

代码示例

Service

    public PageInfo list(int pageNo,int size,int pageShowCount) {

        PageHelper.startPage(pageNo, size);
        List questionList = questionMapper.list();
        PageInfo questionPageInfo = new PageInfo<>(questionList,pageShowCount);

        ListquestionDTOList = new ArrayList<>();
        for(Question question : questionPageInfo.getList()){
            User user = userMapper.findById(question.getCreator());
            QuestionDTO questionDTO = new QuestionDTO();
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }

        PageInfo questionDTOPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(questionPageInfo, questionDTOPageInfo);
        questionDTOPageInfo.setList(questionDTOList);
        return questionDTOPageInfo;
    }

Controller

@GetMapping("/")
    public String hello(HttpServletRequest request, Model model,
                        @RequestParam(name = "page",defaultValue = "1")Integer pageNo,//当前页码
                        @RequestParam(name = "size",defaultValue = "2")Integer size,
                        @RequestParam(name = "pageShowCount",defaultValue = "4")Integer pageShowCount
    ){
        Cookie[] cookies = request.getCookies();
        /*检查浏览器中有没有我们设置的cookie对象*/
        if(cookies != null){
            for(Cookie cookie : cookies){
                if( "token".equals(cookie.getName())){
                    /*根据 cookie 中我们设置的数据来查找数据库中的用户信息*/
                    String  token = cookie.getValue();
                    User user = userMapper.findToken(token);
                    if(user != null){
                        /*根据 cookie找到了用户信息,把它保存到session域中*/
                        request.getSession().setAttribute("user", user);
                    }
                    break;
                }
            }
        }
        

        PageInfo list = questionService.list(pageNo, size, pageShowCount);
        model.addAttribute("questions", list);
        
        return "index";
    }

前端代码-分页条


大坑

在使用pagehelper插件的时候,如果只是对查到的数据进行直接分页(如下),不会有什么问题,

但是,我们经常需要对查询的的数据经行二次操作,就如同我的Service层代码那样,虽然操作后数据正常分页,但pageInfo中的参数基本都不是我们想要的,所以需要一些转变

 PageHelper.startPage(pageNo, size);
 List questionList = questionMapper.list();
 PageInfo questionPageInfo = new PageInfo<>(questionList,pageShowCount);