SpringBoot2.7结合Thymeleaf使用PageHelper实现分页

导入依赖

				<!-- PageHelper分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>
                <!-- 添加thymeleaf依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        

配置application.yml

# PageHelper相关配置
pagehelper:
  helper-dialect: mysql
  
# Thymeleaf相关配置
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8

创建实体类

PageDTO.java

import lombok.Data;

@Data
public class PageDTO {
    private Integer pageNum=1;
    private Integer pageSize=3;
}

Info.java

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class InfoPO {

    private Integer bookId;
    private Integer userId;
    private String bookName;
    private String img;
    private String author;
}

实现controller

ShowInfoController.java

    @GetMapping("/index")
    public String index(Model model,PageDTO pageDTO){
        PageInfo<InfoPO> poList = infoService.findInfos(pageDTO).getData();
        model.addAttribute("page", poList);
        return "index";
    }

编写Service类

InfoService.java

import com.github.pagehelper.PageInfo;
import com.veyit.iot.entity.Result;
import com.veyit.iot.entity.dto.PageDTO;
import com.veyit.iot.entity.po.InfoPO;

import java.util.List;

public interface InfoService {
    Result<PageInfo<InfoPO>> findInfos(PageDTO pageDTO);
}

InfoServiceImpl.java

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.veyit.iot.dao.InfoMapper;
import com.veyit.iot.entity.Result;
import com.veyit.iot.entity.dto.PageDTO;
import com.veyit.iot.entity.po.InfoPO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class InfoServiceImpl implements InfoService {

    @Resource
    private InfoMapper infoMapper;

    @Override
    public Result<PageInfo<InfoPO>> findInfos(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return new Result<>(200,"获取成功",new PageInfo<InfoPO>(infoMapper.getInfos()));
    }
}

编写Dao层

InfoMapper.java

import com.veyit.iot.entity.po.InfoPO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface InfoMapper {
    List<InfoPO> getInfos();
    }

InfoMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.veyit.iot.dao.InfoMapper">
    <select id="getInfos" resultType="InfoPO">
        select * from info
    </select>
</mapper>

前端页面

index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>首页</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
</head>
<body>
<div class="ui container">
    <a class="ui button middle blue" th:href="@{/update}">新增</a>
    <table class="table table-hover" border="1" style="border-spacing: 0 ;width: 80%;">
        <thead>
        <tr>
            <th>id</th>
            <th>发布人id</th>
            <th>书名</th>
            <th>图片</th>
            <th>作者</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr  th:each="info : ${page.list}">
            <td th:text="${info.bookId}">neo</td>
            <td th:text="${info.userId}">neo</td>
            <td th:text="${info.bookName}">neo</td>
            <td th:text="${info.img}">Otto</td>
            <td th:text="${info.author}">6</td>
            <td>
                <div style="text-align: center">
                <a th:href="@{/edit/{id}(id=${info.bookId})}" class="ui button mini pink">修改</a>
                <a th:href="@{/delete/{id}(id=${info.bookId})}" class="ui button mini teal">删除</a>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="ui attached segment" >
        <table class="m-mobile-wide" width="605px">
            <tbody>
            <tr align="center">
                <td>
                    <a th:href="@{/index(pageNum=${page.pageNum}-1)}"  class="ui button basic mini" th:unless="${page.isFirstPage}">上一页</a>
                </td>
                <td><h8 th:text="${page.pageNum}">2</h8>
                    页/共
                    <h8 th:text="${page.pages}">4</h8>
                    页
                    共
                    <h8 th:text="${page.total}">29</h8></td>
                <td>
                    <form name="pageForm" th:action="@{/index}" method="get">
                        <div class="ui mini input ">
                            <input type="text" class="m-bg" name="pageNum" placeholder="页码" style="width: 50px!important; height: 27.76px!important;" required>
                            <button type="submit" style="font-size: 11px!important;width: 30px!important; height: 0px!important; border: none;margin: 5px;padding: 0;" class="button mini">
                                跳转
                            </button>
                        </div>
                    </form>
                </td>
                <td> &nbsp;</td>
                <td  style="float: right">
                    <a th:href="@{/index(pageNum=${page.pageNum}+1)}" class="ui button basic mini " style="float: right;" th:unless="${page.isLastPage}">下一页</a>
                </td>
            </tr>
            </tbody>
        </table>
    </div>

    <div class="ui success  message" th:unless="${#strings.isEmpty(message)}">
        <i class="close icon"></i>
        <div class="header">提示:</div>
        <p th:text="${message}"></p>
    </div>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<script>
    $(".message .close").on('click',function () {
        $(this).closest(".message")
            .transition("fade");
    })
</script>
</html>

效果展示

在这里插入图片描述

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值