当前位置:

首页 > 编程开发 > springboot分页功能怎么实现

springboot分页功能怎么实现

1.分页功能的作用分页功能作为各类网站和系统不可或缺的部分(例如百度搜索结果的分页等),当一个页面数据量大的时候分页作用就体现出来的,其作用有以下5个。(1)减少系统资源的消耗(2)提高数据库的查询性能(3)提升页面的访问速度(4)符合用户的浏览习惯(5)适配页面的排版2.建立测试数据库由于需要实现分页功能,所需的数据较多DROPTABLEIFEXISTStb_user;CREATETABLEtb_user(idint(11)NOTNULLAUTO_INCREMENTCOMMENT'主键id

本文目录

    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;
    
        /**
         *
         * @param totalCount 总记录数
         * @param pageSize 每页记录数
         * @param currPage 当前页数
         * @param 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(),代码如下所示:

    /**
         * 返回分页数据列表
         * 
         * @param pageUtil
         * @return
         */
        List findUsers(PageQueryUtil pageUtil);
    
        /**
         * 返回数据总数
         * 
         * @param pageUtil
         * @return
         */
        int getTotalUser(PageQueryUtil pageUtil);

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

    
        
        
            select id,name,password from tb_user
            order by id desc
            
                limit #{start}.#{limit}
            
        
        
        
            select count(*) from tb_user
        

    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;
    
    @Service
    public class UserService {
    
        @Autowired
        private UserDao userDao;
    
        public PageResult getUserPage(PageQueryUtil pageUtil){
            //当前页面中的数据列表
            List 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 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分页插件所需的源文件,代码如下所示:

    
    
    
    
    
    

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

    
    
    
    
    

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

    springboot分页功能怎么实现

    springboot分页功能怎么实现

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

    springboot分页功能怎么实现

    jqGrid前端页面测试:

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

    
    
    
        
        jqGrid分页测试
        
        
        
    
    
    
        
        
        
        

    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());
        });
    });
    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发
    上一篇: 怎么使用springboot+mybatis拦截器实现水平分表
    下一篇: Linux下怎么部署springboot项目
    相关文章 更多
    springBoot项目常用目录有哪些
    springBoot项目常用目录有哪些

    springBoot项目常用目录springBoot项目的目录结构及名规范介绍基于SpringBoot开发时的目录结构及命名规范,通过介绍能够为你解决,在实际项目中如何规划目录结构?如何命名目录更规范?各个目录分别代表什么含义?等三个问题。目录说明servicex//项目名|-admin-ui//管理服务前端代码(一般将UI和SERVICE放到一个工程中,便于管理)|-servicex-auth//模块1|-servicex-common//模块2|-servicex-gateway//模块3|-serv

    解析SpringBoot和SpringMVC之间的异同
    解析SpringBoot和SpringMVC之间的异同

    解析SpringBoot和SpringMVC之间的异同SpringBoot和SpringMVC是Java领域中非常重要的开发框架。虽然它们都属于Spring框架的一部分,但是在使用和功能上有一些明显的区别。本文将对SpringBoot和SpringMVC进行比较,解析它们之间的异同。首先,让我们来了解一下SpringBoot。SpringBo

    探索SpringCloud和SpringBoot在微服务领域的不同应用方式
    探索SpringCloud和SpringBoot在微服务领域的不同应用方式

    近年来,随着云计算和分布式架构的兴起,微服务架构的应用越来越广泛。而SpringCloud和SpringBoot作为Java开发中的两个重要框架,对于微服务的实现起到了重要的作用。然而,很多人对于它们在微服务领域的不同应用方式还存在一定的疑惑。本文将从不同的角度探索SpringCloud和SpringBoot在微服务中的应用方式。首先,我们来了解一下Spri

    SpringBoot和SpringMVC有何区别?
    SpringBoot和SpringMVC有何区别?

    SpringBoot与SpringMVC的不同之处在哪里?SpringBoot和SpringMVC是两个非常流行的Java开发框架,用于构建Web应用程序。尽管它们经常分别被使用,但它们之间的不同之处也是很明显的。首先,SpringBoot可以被看作是一个Spring框架的扩展或者增强版。它旨在简化Spring应用程序的初始化和配置过程,以帮助开发人

    比较SpringBoot和SpringMVC的异同
    比较SpringBoot和SpringMVC的异同

    SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

    SpringBoot与SpringMVC的不同点是什么?
    SpringBoot与SpringMVC的不同点是什么?

    SpringBoot和SpringMVC是Java开发中常用的两个框架,它们都是由Spring框架所提供的,但在功能和使用方式上有着一些区别。本文将分别介绍SpringBoot和SpringMVC的特点和区别。一、SpringBoot的特点:简化配置:SpringBoot通过约定优于配置的原则,大大简化了项目的配置过程。它可以自动配置项目所需要的参数,开发人

    比较和选择指南:SpringCloud与SpringBoot的功能比较
    比较和选择指南:SpringCloud与SpringBoot的功能比较

    SpringCloud和SpringBoot是目前Java领域中最热门的开源框架,它们分别提供了一套完善的微服务架构和快速构建应用程序的解决方案。本文将对它们的功能进行比较,并给出选择指南,以帮助读者了解它们的优势和适用场景。SpringBoot是一个用于开发Java应用程序的框架,它提供了一个简化的开发流程,集成了大量常用的功能和组件,减少了开发者的工作量

    SpringCloud与SpringBoot:分析它们之间的相似与不同
    SpringCloud与SpringBoot:分析它们之间的相似与不同

    深入剖析SpringCloud和SpringBoot的异同引言:在构建企业级应用程序时,Spring框架已经成为开发者们的首选。它提供了一种简单而强大的方式来构建独立于平台的应用程序。Spring框架由两个核心模块组成:SpringBoot和SpringCloud。虽然它们都可以帮助开发者快速搭建应用程序,但它们的目标和用途存在一些差异。本文将深入剖析Spr

    从架构角度看SpringCloud与SpringBoot的差异
    从架构角度看SpringCloud与SpringBoot的差异

    从架构角度看SpringCloud与SpringBoot的差异引言:在当今的互联网时代,构建分布式系统已经成为了一种必要的需求。而SpringBoot和SpringCloud正是为了满足这个需求而诞生的。尽管它们都是由Spring框架所提供的解决方案,但从架构角度来看,它们存在着一些重要的差异。本文将从架构的角度出发,对SpringBoot和SpringCl

    Java API 开发中使用 SpringBoot 的利弊分析
    Java API 开发中使用 SpringBoot 的利弊分析

    JavaAPI开发中使用SpringBoot的利弊分析随着互联网的快速发展和技术的不断更新迭代,Java技术在开发中的广泛应用引起了人们的重视。而SpringBoot作为一种微服务架构的解决方案,已经逐渐成为Java开发中的热门选择。那么,在JavaAPI开发中使用SpringBoot,到底有哪些利弊呢?下面来分析一下。利:简化开发

    查看更多
    精品专题 更多
    装机必备
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows
    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程
    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    Mac软件 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    Aperty macOS版
    Aperty macOS版
    macOS

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    WINDOWS 更多
    讯飞智作 网页版
    讯飞智作 网页版
    Windows/ macOS/ Linux

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    火山引擎 网页版
    火山引擎 网页版
    Windows/ macOS/ Linux

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    PixTV 网页版
    PixTV 网页版
    Windows

    PixTV 是一款在浏览器中使用的 AI 视觉内容创作平台,提供图片、视频、音频、文字生成、AI 分镜、局部重绘、提示词反推和无限画布等工具,适合短剧、漫剧、广告及社交媒体内容制作。