当前位置:网站首页>Paginate the list collection and display the data on the page
Paginate the list collection and display the data on the page
2022-07-31 05:25:00 【Small fruit ^_^】
Tools
Tool class code:
public class PageUtil {/*** start paging* @param list* @param pageNum page number* @param pageSize how many pieces of data per page* @return*/public static List startPage(List list, Integer pageNum,Integer pageSize) {if (list == null) {return null;}if (list.size() == 0) {return null;}Integer count = list.size(); // total number of recordsInteger pageCount = 0; // page countif (count % pageSize == 0) {pageCount = count / pageSize;} else {pageCount = count / pageSize + 1;}int fromIndex = 0; // start indexint toIndex = 0; // end indexif (pageNum > pageCount){return null;}if (pageNum != pageCount) {System.out.println("pageNum====>"+pageNum);fromIndex = (pageNum - 1) * pageSize;System.out.println("fromindex===>"+fromIndex);toIndex = fromIndex + pageSize;} else {System.out.println("from====>"+fromIndex);fromIndex = (pageNum - 1) * pageSize;toIndex = count;}List pageList = list.subList(fromIndex, toIndex);return pageList;}}
Usage in the project
Data set found from the database: noGroupList noGroup = studentsService.queryStudentNotInGroup(campusId, phone);Call the method of the tool class:List page = PageUtil.startPage(noGroup,pageNum,pageSize);Total number:Integer count = noGroup.size();Total page number:Integer totalPage = count % 10 == 0 ? count / 10 : count / 10 + 1;map.put("totalPage", totalPage);map.put("page", page);
It has been tested and works, I hope it can help you
**Modify it: if there is too much data, if (pageNum != pageCount) this line will be judged wrong, and the test will be!= Modified to!equals---------> if (!pageNum.equals(pageCount)) **
Modification: If the incoming page number is greater than the total page number, a null pointer exception will be reported, so add a judgment:
if (pageNum > pageCount){
return null;
}
Note: This paging tool class is only suitable for a small amount of data. For millions of data, the response time is very long.If there are millions of data, the query takes time, so the response is very slow, and a better method is still under consideration. If you have a better method, please provide it.
边栏推荐
- 城市内涝及桥洞隧道积水在线监测系统
- ABC D - Distinct Trio (Number of k-tuples
- Duplicate entry ‘XXX‘ for key ‘XXX.PRIMARY‘解决方案。
- Unity教程:URP渲染管线实战教程系列【1】
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- MySQL optimization slow log query
- CentOS7 安装MySQL 图文详细教程
- Unity Framework Design Series: How Unity Designs Network Frameworks
- 【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
- MySQL8.0.26安装配置教程(windows 64位)
猜你喜欢
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
打造基于ILRuntime热更新的组件化开发
MySQL forgot password
MySQL-如何分库分表?一看就懂
DVWA shooting range environment construction
MYSQL一站式学习,看完即学完
MySQL事务(transaction) (有这篇就足够了..)
Temporal介绍
【云原生】DevOps(五):集成Harbor
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
随机推荐
centos7安装mysql5.7步骤(图解版)
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
[Detailed explanation of ORACLE Explain]
城市内涝及桥洞隧道积水在线监测系统
A complete introduction to JSqlParse of Sql parsing and conversion
Mysql——字符串函数
Workflow番外篇
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
Summary of MySQL common interview questions (recommended collection!!!)
110 MySQL interview questions and answers (continuously updated)
质量小议12 -- 以测代评
【py脚本】批量二值化处理图像
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
ABC D - Distinct Trio (Number of k-tuples
mysql stored procedure
Go中间件
mysql使用on duplicate key update批量更新数据
Unity手机游戏性能优化系列:针对CPU端的性能调优
Unity resources management series: Unity framework how to resource management