展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > 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项目
    相关文章 更多
    热门文章 更多
    精品专题 更多
    装机必备

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

    Windows

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

    PDF教程

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

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    讯飞智作 网页版
    讯飞智作 网页版

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

    火山引擎 网页版
    火山引擎 网页版

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

    Aperty macOS版
    Aperty macOS版

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

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    讯飞智作 网页版
    讯飞智作 网页版

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

    火山引擎 网页版
    火山引擎 网页版

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

    PixTV 网页版
    PixTV 网页版

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

    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

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

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。