springboot+thymeleaf+pagehelper实现前后台分页功能

思路描述:
后端逻辑:
利用mybatis插件pagehelper实现分页.开启pagehelper之后.根据当前页以及每页显示的条数查询出数据封装到插件定义好的pageInfo对象中并返回到页面.此对象实际上就是一个pagebean对象,包含分页的常规属性,另外查询出的数据都会存储在pageInfo对象中的list属性中;
前端逻辑:
数据遍历时遍历的pageInfo对象中的list属性中的内容;
底部分页栏中的分页页数实现的逻辑可以遍历出所有的页数,pageInfo中有pages属性,表示分页总数;另一种方式可以使用此标签指定显示的分页数:${#numbers.sequence(从哪一页开始,到到哪一页结束);后者相对来讲更灵活.
页面的跳转逻辑都是以当前页为基础进行的.下面贴一下前后端代码:
在这里插入图片描述页面没有显示每页显示的条数,所以就直接从代码中写死了,此处可以动态设置.

分页区域展示代码:

 <ul class="pagination" id="page_ul">
                                                <li th:if="${pageInfo.pageNum!=1}">
                                                    <a th:href="'/gooods/findAll?pageNum='+1">首页</a>
                                                </li>
                                                <li th:if="${pageInfo.hasPreviousPage}">
                                                    <a th:href="'/goods/findAll?pageNum='+${pageInfo.prePage}" aria-label="Previous">
                                                        <span aria-hidden="true">&laquo;</span>
                                                    </a>
                                                </li>
                                                <li th:if="${pageInfo.pageNum!=1}" th:each="page : ${#numbers.sequence(pageInfo.pageNum -3>0?pageInfo.pageNum -3:1,pageInfo.pageNum-1)}">
                                                    <a th:href="'/goods/findAll?pageNum='+${page}" th:text="${page}"></a>
                                                </li>
                                                <li th:if="${pageInfo.pageNum!=pageInfo.pages}">
                                                    <a href="#" style="color: red;" th:text="${pageInfo.pageNum}"></a>
                                                </li>
                                                <li th:if="${pageInfo.pageNum!=pageInfo.pages}" th:each="page : ${#numbers.sequence(pageInfo.pageNum+1,pageInfo.pageNum+3<pageInfo.pages?pageInfo.pageNum+3:pageInfo.pages) }">
                                                    <a th:href="'/goods/findAll?pageNum='+${page}" th:text="${page}"></a>
                                                </li>
                                                <li th:if="${pageInfo.hasNextPage}">
                                                    <a th:href="'/goods/findAll?pageNum='+${pageInfo.nextPage}" aria-label="Next">
                                                        <span aria-hidden="true">&raquo;</span>
                                                    </a>
                                                </li>
                                                <li th:if="${pageInfo.pageNum!=pageInfo.pages}">
                                                    <a th:href="'/gooods/findAll?pageNum='+${pageInfo.pages}">尾页</a>
                                                </li>
                                            </ul>

数据展示代码:

<tr th:each="good : ${pageInfo.list}">
                                        <td th:text="${good.id}">Onions</td>
                                        <td th:text="${good.title}">Onions</td>
                                        <td th:text="${good.categoryName}">Onions</td>
                                        <td th:text="${good.price}">Onions</td>
                                        <td th:text="${good.num}">Onions</td>
                                        <td th:text="${good.updatedStr}">Onions</td>

猜你喜欢

转载自blog.csdn.net/weixin_43401380/article/details/105773273