当前位置:网站首页>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.
边栏推荐
猜你喜欢
Numpy中np.meshgrid的简单用法示例
[C language] Detailed explanation of operators
CentOS7 - yum install mysql
Unity mobile game performance optimization series: performance tuning for the CPU side
【MySQL8入门到精通】基础篇- Linux系统静默安装MySQL,跨版本升级
12 reasons for MySQL slow query
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
限流的原理
A complete introduction to JSqlParse of Sql parsing and conversion
MySQL(更新中)
随机推荐
Mysql application cannot find my.ini file after installation
SQL injection of DVWA
【一起学Rust】Rust学习前准备——注释和格式化输出
MySQL常见面试题汇总(建议收藏!!!)
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
12 reasons for MySQL slow query
PWN ROP
Apache DButils使用注意事项--with modifiers “public“
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
110道 MySQL面试题及答案 (持续更新)
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
Unity URP渲染管线摄像机核心机制剖析
SQL statement to range query time field
MySQL8--Windows下使用压缩包安装的方法
STM32——DMA
【ORACLE Explain 详解】
12个MySQL慢查询的原因分析
【云原生】DevOps(五):集成Harbor