基于springboot的分页插件pagehelper如何使用

背景框架 springboot 和 themleaf引擎 mybatis

插件用法直接从CSDN

https://blog.csdn.net/xingchenyv/article/details/122797602

借鉴模仿

pom文件依赖

<dependency><!--导入分页插件-->
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.4.1</version>
</dependency>

 首先dao层也就是mapper不变 这里的XML文件的sql语句查询也不用limit m,n分页 这是分页插件的前提

List<Student2> queryStudentList(); //mapper接口

service层接口开始改变 返回的是PageInfo这个类

PageInfo<Student2> queryStudentList(Integer pageNum, Integer pageSize);

  service接口实现类 PageHelper开始分页 把查询结果的集合丢给新建对象pagerInfo 

@Autowired
    private StudentMapper studentMapper;

    @Override
    public PageInfo<Student2> queryStudentList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student2> student2 = studentMapper.queryStudentList();
        PageInfo<Student2> pageInfo =new PageInfo<>(student2);
        return pageInfo;
    }

  controller层 需要接收滚动页 通过themleaf引擎把数据展示出去

@Autowired
    private StudentServiceImpl studentService;
    @GetMapping("/")// /请求跳转到首页
    public String queryUserList(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,Model model){
        PageInfo<Student2> student2 = studentService.queryStudentList(pageNum,pageSize);
        model.addAttribute("studentPageInfo",student2);
        return "index";
    }

 前端数据展示代码

<table class="layui-table layui-form">
                                        <thead>
                                        <tr>
                                            <th>学号</th>
                                            <th>姓名</th>
                                            <th>性别</th>
                                            <th>专业名称</th>
                                            <th>所属二级学院</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr th:each="st,itemSta:${studentPageInfo.list}">
                                            <td th:text="${st.getStid()}"></td>
                                            <td th:text="${st.getStName()}"></td>
                                            <td th:text="${st.getStSex()==0?'女':'男'}"></td>
                                            <td th:text="${st.getMajors()}"></td>
                                            <td th:text="${st.getSecCollege}"></td>
                                            <td class="td-manage">
                                                <a title="编辑"  onclick="xadmin.open('编辑','member-edit.html',600,400)" href="javascript:;">
                                                    <i class="layui-icon"></i>
                                                </a>
                                                <a onclick="xadmin.open('修改','member-password.html',600,400)" title="修改" href="javascript:;">
                                                    <i class="layui-icon"></i>
                                                </a>
                                                <a title="删除" onclick="member_del(this,'要删除的id')" href="javascript:;">
                                                    <i class="layui-icon"></i>
                                                </a>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>

  

 

前端分页代码

<div class="page">
                                        <div class="as one">
                                            当前第<span th:text="${studentPageInfo.pageNum}"></span> 页,
                                            共<span th:text="${studentPageInfo.pages}"></span> 页,
                                            <span th:text="${studentPageInfo.total}"></span>条记录
                                            <ul class="as current" style="-style: none;margin-left: 700PX;">
                                                <!--th:if="${studentPageInfo.hasPreviousPage}"}-->
                                                <li th:if="${studentPageInfo.hasPreviousPage}" style="float:left"><!--hasPreviousPage默认值为false, 如果有上一页,则不显示首页-->
                                                    <a th:href="@{/?pageNum=}+${1}">首页</a>
                                                </li>
                                                <li th:if="${studentPageInfo.hasPreviousPage} " style="float:left"><!--hasPreviousPage默认值为false, 如果有上一页,则不显示-->
                                                    <a th:href="@{/?pageNum=}+${studentPageInfo.prePage}">上一页</a>
                                                </li>
                                                <li th:each="nav:${studentPageInfo.navigatepageNums}"style="float:left"><!--navigatepageNums是一个数组:遍历所有导航页号。 -->
                                                    <a th:href="@{/?pageNum=}+${nav}" th:text="${nav}" th:if="${nav != studentPageInfo.pageNum}"></a><!--如果不是当前页则可以跳转其他页面。遍历展示-->
                                                    <a th:class="${'active'}" th:if="${nav == studentPageInfo.pageNum}" th:text="${nav}"></a><!--如果是当前页则有样式。遍历 展示-->
                                                </li>
                                                <li th:if="${studentPageInfo.hasNextPage}"style="float:left"><!--hasNextPage默认值为false, 如果没有下一页,则不显示-->
                                                    <a th:href="@{/?pageNum=}+${studentPageInfo.nextPage}">下一页</a>
                                                </li>
                                                <li th:if="${studentPageInfo.pageNum < studentPageInfo.pages}"style="float:left"><!--如果当前页小于总页数则不显示尾页 -->
                                                    <a th:href="@{/?pageNum=}+${studentPageInfo.pages}">尾页</a>
                                                </li>
                                            </ul>

                                        </div>
                                </div>

  演示结果

 

posted @ 2022-06-18 10:44  中性史莱姆  阅读(166)  评论(0)    收藏  举报