PageHelper分页

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

1.pom.xml添加依赖
<dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper-spring-boot-starter</artifactId>
          <version>1.2.13</version>
</dependency>
2.配置application.yaml
#配置分页
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
3.controller写分页代码
  1. currentPage是当前页,pageSize是当前页的限制大小
  2. List<Type> 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<Type> list = typeService.listPage();
            //封装list到PageInfo对象中,自动分页
            PageInfo<Type> typePageInfo = null;
            if(list!=null){
                typePageInfo =  new PageInfo<>(list);
                list = typePageInfo.getList();
            }
            model.addObject("list",list);
            model.setViewName("admin/types");
            return model;
        }
4.前端
       <table >
            <thead>
            <tr><th>名称</th>
                <th>操作</th>
            </tr></thead>
            <tbody>
            <tr th:each="temp: ${list}">
                <td th:text="${temp.name}"></td>
                <td>
                    <a href="#" class="ui mini teal basic button">编辑</a>
                    <a href="#" class="ui mini red basic button">删除</a>
                </td>
            </tr>
            </tbody>
        </table>
5.结果
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容