springboot+mybatis+thymeleaf+PageHelper实现分页显示

参考

PageHelper文档:https://pagehelper.github.io/docs/howtouse/
大佬文章:https://www.pianshen.com/article/6213345123/

此demo已上传至github:https://github.com/ktsrkw/springboot-examples/tree/main/springboot-mybatis-pagehelper

具体步骤

1.1 导入依赖
从maven仓库搜

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>

1.2 application.propertise

pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

1.3 Controller

package com.wt.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wt.mapper.GoodsMapper;
import com.wt.mapper.UserMapper;
import com.wt.pojo.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class TestController {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    UserMapper userMapper;

    @GetMapping({"/","/index"})
    public String index(Model model,
                        @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                        @RequestParam(defaultValue="3",value="pageSize")Integer pageSize){
//        List<Goods> goods = goodsMapper.getAllGoods();
//        model.addAttribute("goods",goods);

        //为了程序的严谨性,判断非空:
        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 3;    //设置默认每页显示的数据数
        }

        //1.引入分页插件,pageNum是第几页,pageSize是每页显示多少条,默认查询总数count
        //既获取第pageNum页,pageSize条内容
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页,除非再次调用PageHelper.startPage
        List<Goods> goods = goodsMapper.getAllGoods();
        //3.使用PageInfo包装查询后的结果,pageSize是连续显示的条数,结果list类型是Page<E>
//        PageInfo<Goods> pageInfo = new PageInfo<Goods>(goods,pageSize);
        PageInfo pageInfo = new PageInfo(goods);
        //4.使用model/map/modelandview等带回前端
        model.addAttribute("goods",goods);
        model.addAttribute("pageInfo",pageInfo);

        return "index";
    }
}

1.4 thymeleaf

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>Here is Index</h1>
<p>
    分类
    <a th:href="@{/goods/studysupplies}">学习用品</a>
    <a th:href="@{/goods/transportation}">交通工具</a>
    <a th:href="@{/goods/dailynecessities}">生活日用品</a>
    <a th:href="@{/goods/clothing}">衣物</a>
    <a th:href="@{/goods/electronicproduct}">电子产品</a>
    <a th:href="@{/goods/books}">书籍</a>
    <a th:href="@{/goods/othergoods}">其他</a>
    <a th:href="@{/index}">全部</a>
</p>

<form th:action="@{/goods/search}">
    <input type="text" name="searchContent">
    <button type="submit">搜索</button>
</form>

<table>
    <thead>
    <tr>
        <th>名称</th>
        <th>购入时价格</th>
        <th>转卖价格</th>
        <th>分类</th>
        <th>详细信息</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="everygoods:${goods}">
        <td th:text="${everygoods.getTitle()}"></td>
        <td th:text="¥+${everygoods.getOriginalPrice()}"></td>
        <td th:text="¥+${everygoods.getPrice()}"></td>
        <td th:text="${everygoods.getCategory()}"></td>
        <td>
            <a th:href="@{/goods/}+${everygoods.getGoodsid()}">查看</a>
        </td>
    </tr>
    </tbody>
</table>

<p>
    当前第 [[${pageInfo.pageNum}]]页,共 [[${pageInfo.pages}]] 页.一共 [[${pageInfo.total}]] 条记录
</p>

<ul>
    <!--如果当前页之前有页面,显示回到首页按钮-->
    <li th:if="${pageInfo.hasPreviousPage}">
        <a th:href="'/index?pageNum=1'">首页</a>
    </li>

    <!--如果当前页之前有页面,显示回到前一页按钮-->
    <li th:if="${pageInfo.hasPreviousPage}">
        <a th:href="'/index?pageNum='+${pageInfo.prePage}">
            前一页
        </a>
    </li>

    <!--遍历条数-->
    <li th:each="nav:${pageInfo.navigatepageNums}">
        <a th:href="'/index?pageNum='+${nav}" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a>
        <span th:if="${nav == pageInfo.pageNum}" th:text="${nav}" ></span>
    </li>

    <!--如果当前页之后有页面,显示下一页按钮-->
    <li th:if="${pageInfo.hasNextPage}">
        <a th:href="'/index?pageNum='+${pageInfo.nextPage}">
            下一页
        </a>
    </li>

    <!--如果当前页之后有页面,显示尾页按钮-->
    <li th:if="${pageInfo.hasNextPage}">
        <a th:href="'/index?pageNum='+${pageInfo.pages}">尾页</a>
    </li>
</ul>

</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值