springboot实现分页功能

引入pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.huyangfeng</groupId>
    <artifactId>springboot-student-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-student-crud</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--JDBC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>-->
        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--mybatis整合springboot-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <!--<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>-->
        <!--mysql连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--lombok:偷懒神器-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency><!--导入分页插件-->
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

实体

接口

 Service

 mapper.xml

 ServiceImpl 

package com.xc.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.mapper.Studentmapper;
import com.xc.pojo.Student;
import com.xc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 星晨
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private Studentmapper studentmapper;

    @Override
    public PageInfo<Student> queryAllByPage(Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentmapper.queryAllByPage();
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        return pageInfo;
    }
}

Controller

package com.xc.controller;

import com.github.pagehelper.PageInfo;
import com.xc.pojo.Student;
import com.xc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 星晨
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/list")
    public String queryAllByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                 Model model){
        PageInfo<Student> studentPageInfo = studentService.queryAllByPage(pageNum, pageSize);
//        System.out.println(studentPageInfo);
//        System.out.println(studentPageInfo.getList());
//        System.out.println(studentPageInfo.getList().indexOf(1)+1);
        model.addAttribute("studentPageInfo",studentPageInfo);
        return "list";
    }

}

前端

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" >
</head>

<body>
<div class="box">
    <div class="insert">
    </div>
    <table class="table table-hover" border="1" cellspacing="0" cellpadding="0">
        <thead >
        <tr >
            <th class="text-center">学生ID</th>
            <th class="text-center">学生姓名</th>
            <th class="text-center">学生密码</th>
            <th class="text-center">学生年龄</th>
            <th class="text-center">学生性别</th>
            <th class="text-center">操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="st,itemSta:${studentPageInfo.list}">
            <td th:text="${itemSta.index+1}"></td>
            <td th:text="${st.studentname}"></td>
            <td th:text="${st.studentpswd}"></td>
            <td th:text="${st.studentage}"></td>
            <td th:text="${st.studentsex==1?'男':'女'}"></td>
        </tr>
        </tbody>
    </table>
</div>

<div class="as">
    <div class="as one">
        当前第<span th:text="${studentPageInfo.pageNum}"></span> 页,
        共<span th:text="${studentPageInfo.pages}"></span> 页,
        <span th:text="${studentPageInfo.total}"></span>条记录
    </div>
    <ul class="as current" style="list-style: none">
        <!--th:if="${studentPageInfo.hasPreviousPage}"}-->
        <li th:if="${studentPageInfo.hasPreviousPage}"}><!--hasPreviousPage默认值为false, 如果有上一页,则不显示首页-->
            <a th:href="@{/list?pageNum=}+${1}">首页</a>
        </li>
        <li th:if="${studentPageInfo.hasPreviousPage}"><!--hasPreviousPage默认值为false, 如果有上一页,则不显示-->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.prePage}">上一页</a>
        </li>
        <li th:each="nav:${studentPageInfo.navigatepageNums}"><!--navigatepageNums是一个数组:遍历所有导航页号。 -->
            <a th:href="@{/list?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}"><!--hasNextPage默认值为false, 如果没有下一页,则不显示-->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.nextPage}">下一页</a>
        </li>
        <li th:if="${studentPageInfo.pageNum < studentPageInfo.pages}"><!--如果当前页小于总页数则不显示尾页 -->
            <a th:href="@{/list?pageNum=}+${studentPageInfo.pages}">尾页</a>
        </li>
    </ul>
</div>



</body>
</html>
<style>
    .table-hover{
        text-align: center;
    }
    .as .current{
        width: 600px;
        text-align: center;
    }
    ul::after{
        content:"";
        display:block;
        height:0;
        clear:both;
        visibility:hidden;
    }
    ul{
        margin-left: -35px;
    }
    .one{
        margin-left: 15px;
    }
    ul a{
        text-decoration: none;
        display: inline-block;
        width: 50px;
        height: 30px;
        line-height: 30px;
        text-align: center;

    }
    ul li{
        float: left;
        background: #ffc0cb;
        list-style: none;
        margin: 10px;
        border-radius: 10px;
    }
    .active{
        background-color: orange;
        border-radius: 10px;
    }

</style>

 效果

  • 36
    点赞
  • 181
    收藏
    觉得还不错? 一键收藏
  • 32
    评论
### 回答1: 实现 springboot 和 layui 表格分页的步骤如下: 1. 前端页面:在使用 layui 表格的页面引入 layui.js 和 layui.css 文件。 2. 后端代码:使用 springboot 构建后端服务,通过 HTTP 请求向前端发送数据。 3. 分页参数:在前端页面,使用 layui table 控件定义分页参数,例如:每页显示数量、当前页码等。 4. 分页请求:使用 layui table 控件发送请求到后端,后端通过分页参数从数据库中查询相应的数据并返回给前端。 5. 表格渲染:前端接收到后端返回的数据,使用 layui table 控件将数据渲染到表格中,实现表格分页功能。 6. 翻页事件:使用 layui table 控件监听翻页事件,在事件触发时重新发送请求到后端,实现下一页或上一页的数据加载。 希望以上步骤能帮助您实现 springboot 和 layui 表格分页。 ### 回答2: 在SpringBoot和Layui实现表格分页的过程中,可以通过以下步骤实现: 1. 在后端代码中,首先引入Layui的DataTable插件,使用Layui提供的数据表格组件实现分页效果。 2. 在后端代码中,通过SpringBoot的控制器(Controller)接收前端传递的分页参数,例如当前页码(page)和每页显示数量(limit)。 3. 在后端代码中,根据接收到的分页参数,利用数据库查询语句查询数据表中的数据,并根据传递的分页参数进行分页操作。 4. 将查询到的分页数据返回给前端。 5. 在前端代码中,使用Layui的table模块生成表格,并设置分页参数。 6. 通过JavaScript在前端代码中调用Layui的table模块提供的分页方法,根据后端返回的分页数据动态生成分页组件。 7. 在前端代码中,利用JavaScript将查询到的数据填充到表格的相应位置。 总结来说,SpringBoot和Layui实现表格分页的核心是后端对分页参数的接收和数据的分页操作,以及前端对后端返回的分页数据的处理和分页组件的生成。通过这样的操作,可以实现便捷的表格分页功能。 ### 回答3: 在Spring Boot和Layui结合使用时,实现表格分页可以通过以下步骤来实现: 1. 在后端使用Spring Boot开发接口,查询数据库获取分页数据。可以使用Spring Data JPA或MyBatis等持久层框架进行数据库操作。 2. 定义一个Java类来表示表格数据的结果集,包含总记录数和当前页数据列表。 3. 在后端接口中,根据前端传递的页码和每页显示的记录数,通过查询条件进行筛选,并使用分页查询语句获取对应页码的数据。 4. 将查询结果封装到步骤2中定义的实体类中,并返回给前端。 5. 在前端的HTML页面中引入Layui框架的相关CSS和JS文件。 6. 在HTML页面中使用Layui提供的表格组件,结合异步加载数据的方式进行数据展示和分页操作。 7. 在Layui的表格组件中,配置分页参数,包括每页显示的记录数、总记录数以及分页样式等。 8. 在Layui的表格组件的回调事件中,监听分页操作,并通过Ajax请求后端接口获取指定页的数据,并将返回的数据展示到表格中。 通过以上步骤,可以实现Spring Boot和Layui结合使用的表格分页功能。需要注意的是,在后端接口中,除了分页查询外,还需要进行条件查询、排序等操作,以满足不同的业务需求。同时,前端页面中还可以添加筛选条件、搜索功能等,以提高用户体验。
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值