spring boot整合pageHelper分页插件

pageHelper是一个很好的分页插件,可以帮助我们方便快捷的完成数据分页操作,减轻工作量,而且简单易用。

1.导入依赖

    
            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelper-spring-boot-starterartifactId>
                <version>1.2.5version>
            dependency>

2.配置分页插件

    #配置分页插件
    pagehelper.helper-dialect=mysql
    pagehelper.params=count=countSql
    pagehelper.reasonable=true
    pagehelper.support-methods-arguments=true

3.编写相应接口

    @Select("select * from student_info")
    List<Student> selectAll();

4.service层

调用分页 Page page = PageHelper.startPage(2,2);参数分别是(当前页码,每页记录数)

    //整合pageHelper分页插件
        public List<Student> selectAll(){
            //设置当前所在页面和每页数据数目,在调用查询方法前调用
            Page<Object> page = PageHelper.startPage(2,2);
            //page的相关方法
            System.out.println("每页记录数:"+page.getPageSize());
            System.out.println("总记录数:"+page.getTotal());
            System.out.println("当前页码:"+page.getPageNum());
            System.out.println("结果集:"+page.getResult());
            List<Student> list = studentDao.selectAll();
            //pageInfo封装页面信息
            PageInfo pageInfo = new PageInfo(list);
            System.out.println("结果集:"+pageInfo.getList());
            System.out.println("当前页面:"+pageInfo.getPageNum());
            System.out.println("总页数:"+pageInfo.getPages());
            System.out.println("当前页面显示的数据条目:"+pageInfo.getPageSize());
            System.out.println("下一页:"+pageInfo.getNextPage());
            System.out.println("上一页:"+pageInfo.getPrePage());
            System.out.println("是否有下一页:"+pageInfo.isHasNextPage());
            System.out.println("是否有上一页:"+pageInfo.isHasPreviousPage());
            return list;
        }

5.controller层

     @GetMapping("/selectAll")
        public Object selectAll(){
            return studentService.selectAll();
        }

6.测试

在这里插入图片描述
spring boot整合pageHelper分页插件_第1张图片

7.扩展

还可以利用pageInfo向前台传输数据

     @GetMapping("/selectAll")
        public String selectAll(Model model){
            List<Student> list = studentService.selectAll();
            PageInfo pageInfo = new PageInfo(list);
            //利用pageInfo封装的信息,向前台传输数据
            model.addAttribute("pageinfo",pageInfo);
            return "list";
        }

前台list页面

    
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    <div align="center">
        <table border="1">
            <tr>
                <th>idth>
                <th>nameth>
                <th>sexth>
                <th>ageth>
            tr>
            <tr th:each="person:${pageInfo.list}">
                <td th:text="${person.id}">td>
                <td th:text="${person.name}">td>
                <td th:text="${person.sex}">td>
                <td th:text="${person.age}">td>
            tr>
        table>
        <p>当前 <span th:text="${pageInfo.pageNum}">span> 页,总 <span th:text="${pageInfo.pages}">span> 页,共 <span th:text="${pageInfo.total}">span> 条记录p>
        <a th:href="@{/getAllPerson}">首页a>
        <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页a>
        <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页a>
        <a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页a>
    div>
    body>
    html>

你可能感兴趣的:(spring,boot)