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

目录

  • 导入依赖
  • 配置application.yml
  • 创建实体类
  • 实现controller
  • 编写Service类
  • 编写Dao层
  • 前端页面
  • 效果展示

导入依赖

				
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelper-spring-boot-starterartifactId>
            <version>1.4.1version>
        dependency>
                
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        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


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/[email protected]/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>idth>
            <th>发布人idth>
            <th>书名th>
            <th>图片th>
            <th>作者th>
            <th>操作th>
        tr>
        thead>
        <tbody>
        <tr  th:each="info : ${page.list}">
            <td th:text="${info.bookId}">neotd>
            <td th:text="${info.userId}">neotd>
            <td th:text="${info.bookName}">neotd>
            <td th:text="${info.img}">Ottotd>
            <td th:text="${info.author}">6td>
            <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}">2h8>
                    页/共
                    <h8 th:text="${page.pages}">4h8>
                    页
                    共
                    <h8 th:text="${page.total}">29h8>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>  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/[email protected]/dist/semantic.min.js">script>
<script>
    $(".message .close").on('click',function () {
        $(this).closest(".message")
            .transition("fade");
    })
script>
html>

效果展示

SpringBoot2.7结合Thymeleaf使用PageHelper实现分页_第1张图片

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