iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >springboot分页功能怎么实现
  • 474
分享到

springboot分页功能怎么实现

2023-07-06 04:07:43 474人浏览 安东尼
摘要

这篇“SpringBoot分页功能怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springboot分页功能怎么实现

这篇“SpringBoot分页功能怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springboot分页功能怎么实现”文章吧。

1.分页功能的作用

分页功能作为各类网站和系统不可或缺的部分(例如百度搜索结果的分页等),当一个页面数据量大的时候分页作用就体现出来的,其作用有以下5个。

(1)减少系统资源的消耗

(2)提高数据库的查询性能

(3)提升页面的访问速度

(4)符合用户的浏览习惯

(5)适配页面的排版

2.建立测试数据库

由于需要实现分页功能,所需的数据较多

DROP TABLE IF EXISTS tb_user;CREATE TABLE tb_user (    id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键id',    name varchar(100) NOT NULL DEFAULT '' COMMENT '登录名',    passWord varchar(100) NOT NULL DEFAULT '' COMMENT '密码',    PRIMARY KEY (id) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8;insert into tb_user (id,name,password)value (1,'C','123456'),(2,'c++','123456'),(3,'Java','123456'),(4,'python','123456'),(5,'R','123456'),(6,'C#','123456');insert into tb_user (id,name,password) value (7,'test1','123456');insert into tb_user (id,name,password) value (8,'test2','123456');insert into tb_user (id,name,password) value (9,'test3','123456');insert into tb_user (id,name,password) value (10,'test4','123456');insert into tb_user (id,name,password) value (11,'test5','123456');insert into tb_user (id,name,password) value (12,'test6','123456');insert into tb_user (id,name,password) value (13,'test7','123456');insert into tb_user (id,name,password) value (14,'test8','123456');insert into tb_user (id,name,password) value (15,'test9','123456');insert into tb_user (id,name,password) value (16,'test10','123456');insert into tb_user (id,name,password) value (17,'test11','123456');insert into tb_user (id,name,password) value (18,'test12','123456');insert into tb_user (id,name,password) value (19,'test13','123456');insert into tb_user (id,name,password) value (20,'test14','123456');insert into tb_user (id,name,password) value (21,'test15','123456');insert into tb_user (id,name,password) value (22,'test16','123456');insert into tb_user (id,name,password) value (23,'test17','123456');insert into tb_user (id,name,password) value (24,'test18','123456');insert into tb_user (id,name,password) value (25,'test19','123456');insert into tb_user (id,name,password) value (26,'test20','123456');insert into tb_user (id,name,password) value (27,'test21','123456');insert into tb_user (id,name,password) value (28,'test22','123456');insert into tb_user (id,name,password) value (29,'test23','123456');insert into tb_user (id,name,password) value (30,'test24','123456');insert into tb_user (id,name,password) value (31,'test25','123456');insert into tb_user (id,name,password) value (32,'test26','123456');insert into tb_user (id,name,password) value (33,'test27','123456');insert into tb_user (id,name,password) value (34,'test28','123456');insert into tb_user (id,name,password) value (35,'test29','123456');insert into tb_user (id,name,password) value (36,'test30','123456');insert into tb_user (id,name,password) value (37,'test31','123456');insert into tb_user (id,name,password) value (38,'test32','123456');insert into tb_user (id,name,password) value (39,'test33','123456');insert into tb_user (id,name,password) value (40,'test34','123456');insert into tb_user (id,name,password) value (41,'test35','123456');insert into tb_user (id,name,password) value (42,'test36','123456');insert into tb_user (id,name,password) value (43,'test37','123456');insert into tb_user (id,name,password) value (44,'test38','123456');insert into tb_user (id,name,password) value (45,'test39','123456');insert into tb_user (id,name,password) value (46,'test40','123456');insert into tb_user (id,name,password) value (47,'test41','123456');insert into tb_user (id,name,password) value (48,'test42','123456');insert into tb_user (id,name,password) value (49,'test43','123456');insert into tb_user (id,name,password) value (50,'test44','123456');insert into tb_user (id,name,password) value (51,'test45','123456');

3.分页功能返回的结果封装

新建一个util包并在包中新建Result通用结果类,代码如下:

package ltd.newbee.mall.entity;public class User {    private Integer id;    private String name;    private String password;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

后端接口返回的数据会根据以上格式进行数据封装,包括业务码、返回信息、实际的数据结果。这个格式是开发人员自行设置的,如果有其他更好的方案也可以进行适当的调整。

在util包中新建PageResult通用结果类,代码如下:

package ltd.newbee.mall.util;import java.util.List;public class PageResult {    //总记录数    private int totalCount;    //每页记录数    private int pageSize;    //总页数    private int totalPage;    //当前页数    private int currPage;    //列表数据    private List<?> list;        public PageResult(int totalCount, int pageSize, int currPage, List<?> list) {        this.totalCount = totalCount;        this.pageSize = pageSize;        this.currPage = currPage;        this.list = list;        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);    }    public int getTotalCount() {        return totalCount;    }    public void setTotalCount(int totalCount) {        this.totalCount = totalCount;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getTotalPage() {        return totalPage;    }    public void setTotalPage(int totalPage) {        this.totalPage = totalPage;    }    public int getCurrPage() {        return currPage;    }    public void setCurrPage(int currPage) {        this.currPage = currPage;    }    public List<?> getList() {        return list;    }    public void setList(List<?> list) {        this.list = list;    }}

4.分页功能代码具体实现

4.1数据层

在UserDao接口中新增两个方法findUsers()和getTotalUser(),代码如下所示:

    List<User> findUsers(PageQueryUtil pageUtil);        int getTotalUser(PageQueryUtil pageUtil);

在UserMapper.xml文件中新增这两个方法的映射语句,代码如下所示:

<!--分页-->    <!--查询用户列表-->    <select id="findUsers" parameterType="Map" resultMap="UserResult">        select id,name,password from tb_user        order by id desc        <if test="start!=null and limit!=null">            limit #{start}.#{limit}        </if>    </select>    <!--查询用户总数-->    <select id="getTotalUser" parameterType="Map" resultType="int">        select count(*) from tb_user    </select>

4.2业务层

新建service包,并新增业务类UserService,代码如下所示:

import ltd.newbee.mall.dao.UserDao;import ltd.newbee.mall.entity.User;import ltd.newbee.mall.util.PageResult;import ltd.newbee.mall.util.PageQueryUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserService {    @Autowired    private UserDao userDao;    public PageResult getUserPage(PageQueryUtil pageUtil){        //当前页面中的数据列表        List<User> users = userDao.findUsers(pageUtil);        //数据总条数,用于计算分页数据        int total = userDao.getTotalUser(pageUtil);        //分页信息封装        PageResult pageResult = new PageResult(users,total,pageUtil.getLimit(),pageUtil.getPage());        return pageResult;    }}

首先根据当前页面和每页条数查询当前页的数据集合,然后调用select count(*)语句查询数据的总条数用于计算分页数据,最后将获取的数据封装到PageResult对象中并返回给控制层。

4.3控制层

在controller包中新建PageTestController类,用于实现分页请求的处理并返回查询结果,代码如下所示:

@RestController@RequestMapping("users")public class PageTestController {    @Autowired    private UserService userService;    //分页功能测试    @RequestMapping(value = "/list",method = RequestMethod.GET)    public Result list(@RequestParam Map<String,Object> params){        Result result = new Result();        if (StringUtils.isEmpty(params.get("page"))||StringUtils.isEmpty(params.get("limit"))){            //返回错误码            result.setResultCode(500);            //错误信息            result.setMessage("参数异常!");            return result;        }        //封装查询参数        PageQueryUtil queryParamList = new PageQueryUtil(params);        //查询并封装分页结果集        PageResult userPage = userService.getUserPage(queryParamList);        //返回成功码        result.setResultCode(200);        result.setMessage("查询成功");        //返回分页数据        result.setData(userPage);        return result;    }}

分页功能的交互流程:前端将所需页码和条数参数传输给后端,后端在接收分页请求后对分页参数进行计算,并利用MySQL的limit关键字查询对应的记录,在查询结果被封装后返回给前端。在TestUserControler类上使用的是@RestController注解,该注解相当于@ResponseBody+@Controller的组合注解。

5.jqGrid分页插件

jqGrid是一个用来显示网格数据的Jquery插件。开发人员通过使用jqGrid可以轻松实现前端页面与后台数据的ajax异步通信并实现分页功能。同时,jqGrid是一款代码开源的分页插件,源码也一直处于迭代更新的状态中。

下载地址:jqGrid

下载jqGrid后解压文件,将解压的文件直接拖进项目的static目录下

springboot分页功能怎么实现

以下是jqGrid实现分页的步骤:

首先,在前端页面代码中引入jqGrid分页插件所需的源文件,代码如下所示:

<link href="plugins/jqgrid-5.8.2/ui.jqgrid-bootstrap4.CSS" rel="external nofollow"  rel="stylesheet"/><!--jqGrid依赖jQuery,因此需要先引入jquery.min.js文件,下方地址为字节跳动提供的cdn地址--><script src="Http://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script><!--grid.locale-cn.js为国际化所需的文件,-cn表示中文--><script src="plugins/jqgrid-5.8.2/grid.locale-cn.js"></script><script src="plugins/jqgrid-5.8.2/jquery.jqGrid.min.js"></script>

其次,在页面中需要展示分页数据的区域添加用于jqGrid初始化的代码:

<!--jqGrid必要DOM,用于创建表格展示列表数据--><table id="jqGrid" class="table table-bordered"></table><!--jqGrid必要DOM,分页信息区域--><div id="jqGridPager"></div>

最后,调用jqGrid分页插件的jqGrid()方法渲染分页展示区域,代码如下所示:

springboot分页功能怎么实现

springboot分页功能怎么实现

jqGrid()方法中的参数及含义如图所示。

springboot分页功能怎么实现

jqGrid前端页面测试:

在resources/static目中新建jqgrid-page-test.html文件,代码如下所示:

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>jqGrid分页测试</title>    <!--引入bootstrap样式文件-->    <link rel="stylesheet" href="/static/bootstrap-5.3.0-alpha3-dist/css/bootstrap.css" rel="external nofollow" />    <link href="jqGrid-5.8.2/css/ui.jqgrid-bootstrap4.css" rel="external nofollow"  rel="stylesheet"/></head><body><div >    <!--数据展示列表,id为jqGrid-->    <table id="jqGrid" class="table table-bordered"></table>    <!--分页按钮展示区-->    <div id="jqGridPager"></div></div></body><!--jqGrid依赖jQuery,因此需要先引入jquery.min.js文件,下方地址为字节跳动提供的cdn地址--><script src="http://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script><!--grid.locale-cn.js为国际化所需的文件,-cn表示中文--><script src="plugins/jqgrid-5.8.2/grid.locale-cn.js"></script><script src="plugins/jqgrid-5.8.2/jquery.jqGrid.min.js"></script><script src="jqgrid-page-test.js"></script></html>

jqGrid初始化

在resources/static目录下新建jqgrid-page-test.js文件,代码如下所示:

$(function () {    $("#jqGrid").jqGrid({        url: 'users/list',        datatype: "JSON",        colModel: [            {label: 'id',name: 'id', index: 'id', width: 50, hidden: true,key:true},            {label: '登录名',name: 'name',index: 'name', sortable: false, width: 80},            {label: '密码字段',name: 'password',index: 'password', sortable: false, width: 80}        ],        height: 485,        rowNum: 10,        rowList: [10,30,50],        styleUI: 'Bootstrap',        loadtext: '信息读取中...',        rownumbers: true,        rownumWidth: 35,        autowidth: true,        multiselect: true,        pager: "#jqGridPager",        jsonReader:{            root: "data.list",            page: "data.currPage",            total: "data.totalCount"        },        prmNames:{            page: "page",            rows: "limit",            order: "order"        },        gridComplete: function () {            //隐藏grid底部滚动条            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});        }    });    $(window).resize(function () {        $("jqGrid").setGridWidth($(".card-body").width());    });});

以上就是关于“springboot分页功能怎么实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: springboot分页功能怎么实现

本文链接: https://www.lsjlt.com/news/357734.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springboot分页功能怎么实现
    这篇“springboot分页功能怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springboot分页功能怎么实现...
    99+
    2023-07-06
  • SpringBoot实现分页功能
    本文实例为大家分享了SpringBoot实现分页功能的具体代码,供大家参考,具体内容如下 新建demo\src\main\java\com\example\demo\mapper\U...
    99+
    2022-11-12
  • VUE+SpringBoot实现分页功能
    本文主要介绍一下 Vue + SpringBoot 中如何实现一个分页列表数据。 1、效果展示 2、VUE代码 VUE之视图定义 <el-row> ...
    99+
    2022-11-12
  • 怎么在VUE中使用SpringBoot实现分页功能
    这篇文章给大家介绍怎么在VUE中使用SpringBoot实现分页功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1、效果展示2、VUE代码VUE之视图定义<el-row>   ...
    99+
    2023-06-15
  • Springboot+Mybatis怎么实现分页加条件查询功能
    本篇内容介绍了“Springboot+Mybatis怎么实现分页加条件查询功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!User.xml...
    99+
    2023-06-30
  • SpringBoot实现转页功能
    目录内部转页forward转页配置准备页面在方法中添加转页得到页面路径测试重新定向redirect添加新的方法测试简单转页页面发请求的三种方式<a>超链接form表单lo...
    99+
    2023-05-20
    SpringBoot转页 SpringBoot页面跳转
  • css怎么实现分页功能
    css实现分页功能的方法:1、创建html文件;2、添加html代码架构;3、在body标签中使用ul、li标签设计页码数;4、添加script标签并写入css样式代码来实现点击及鼠标悬停的分页样式;5、通过浏览器方式查看设计效果。具体操作...
    99+
    2022-10-16
  • html怎么实现分页功能
    html中实现分页功能的方法:1、创建html文件;2、添加html代码架构;3、在body标签中使用ul、li标签设计页码数;4、添加script标签并写入css样式代码来实现点击及鼠标悬停的分页样式;5、通过浏览器方式查看设计效果。具体...
    99+
    2022-10-14
  • java怎么实现分页功能
    这篇文章主要介绍“java怎么实现分页功能”,在日常操作中,相信很多人在java怎么实现分页功能问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java怎么实现分页功能”的疑惑有所帮助!接下来,请跟着小编一起来...
    99+
    2023-06-03
  • gridview分页功能怎么实现
    GridView分页功能的实现步骤如下:1. 在前端页面中添加一个GridView控件,并将其属性`AllowPaging`设置为`...
    99+
    2023-08-31
    gridview
  • SpringBoot分页查询功能的实现方法
    目录前言:首先是手动实现分页查询:接下来是关联前端分页和后台数据:总结前言: 学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流...
    99+
    2022-11-13
  • springboot实现分页功能的完整代码
    目录1.分页功能的作用2.建立测试数据库3.分页功能返回的结果封装4.分页功能代码具体实现4.1数据层4.2业务层4.3控制层5.jqGrid分页插件总结1.分页功能的作用 分页功能...
    99+
    2023-05-16
    springboot分页 springboot分页条件查询 springboot分页功能
  • Ajax中怎么实现分页功能
    Ajax中怎么实现分页功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。首先创建前台页面MyAjaxPager.aspx<%@ ...
    99+
    2022-10-19
  • 怎么用AJAX实现分页功能
    本篇内容主要讲解“怎么用AJAX实现分页功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用AJAX实现分页功能”吧!需要最新版本的bingo.js支持,下...
    99+
    2022-10-19
  • Springboot+Mybatis实现分页加条件查询功能
    本文实例为大家分享了Springboot+Mybatis实现分页加条件查询的具体代码,供大家参考,具体内容如下 User.xml <xml version="1.0" enco...
    99+
    2022-11-13
  • JavaWeb分页查询功能怎么实现
    本篇内容主要讲解“JavaWeb分页查询功能怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JavaWeb分页查询功能怎么实现”吧!效果:实现:分页查询有几个比较重要的参数,pageNum...
    99+
    2023-06-26
  • php怎么实现数组分页功能
    本篇内容主要讲解“php怎么实现数组分页功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php怎么实现数组分页功能”吧!在php中可以通过数组分页函数array_slice()来实现分页功能,...
    99+
    2023-06-20
  • SpringBoot如何整合PageHelper实现分页查询功能
    这篇文章主要介绍了SpringBoot如何整合PageHelper实现分页查询功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。使用方法导入依赖在中央仓库sonatype中搜...
    99+
    2023-06-29
  • SpringBoot整合PageHelper实现分页查询功能详解
    前言 本文介绍的是MyBatis 分页插件 PageHelper,如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件。分页插件支持任何复杂的单表、多表分页...
    99+
    2022-11-13
  • 怎么在springboot中利用thymeleaf模板实现一个paginate分页功能
    今天就跟大家聊聊有关怎么在springboot中利用thymeleaf模板实现一个paginate分页功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。pom.xml 加入 ...
    99+
    2023-05-31
    springboot thymeleaf paginate
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作