PageHelper分页

环境:springboot+mybatis+maven+yaml配置+前端用thymleaf模板(其实环境不太一样也可以,我觉得我这种方法很简单)

1.pom.xml添加依赖

          com.github.pagehelper
          pagehelper-spring-boot-starter
          1.2.13

2.配置application.yaml
#配置分页
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
3.controller写分页代码
  1. currentPage是当前页,pageSize是当前页的限制大小
  2. List list = typeService.listPage();是从数据库中获取所有的数据行,这个时候还没有分页。
  3. 然后如果list!=null。这个时候就把list放到PageInfo类型的变量中,然后才从PageInfo中获得list,就完成了分页。
  4. 把list返回前端。
@RequestMapping("/type")
    public ModelAndView getTypes(@RequestParam(defaultValue = "1") Integer currentPage,
                                   @RequestParam(defaultValue = "1") Integer pageSize){
            ModelAndView model = new ModelAndView();
            //1.开启分页
            PageHelper.startPage(currentPage,pageSize);
            List list = typeService.listPage();
            //封装list到PageInfo对象中,自动分页
            PageInfo typePageInfo = null;
            if(list!=null){
                typePageInfo =  new PageInfo<>(list);
                list = typePageInfo.getList();
            }
            model.addObject("list",list);
            model.setViewName("admin/types");
            return model;
        }
4.前端
       
名称 操作
编辑 删除
5.结果

你可能感兴趣的:(PageHelper分页)