当前位置:网站首页>对list集合进行分页,并将数据显示在页面中
对list集合进行分页,并将数据显示在页面中
2022-07-31 05:08:00 【小果子^_^】
工具类
工具类代码:
public class PageUtil {
/**
* 开始分页
* @param list
* @param pageNum 页码
* @param pageSize 每页多少条数据
* @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(); // 记录总数
Integer pageCount = 0; // 页数
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0; // 开始索引
int toIndex = 0; // 结束索引
if (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;
}
}
在项目中的用法
从数据库中查出的数据集合:noGroup
List<Students> noGroup = studentsService.queryStudentNotInGroup(campusId, phone);
调用工具类的方法:
List<Students> page = PageUtil.startPage(noGroup,pageNum,pageSize);
总条数:
Integer count = noGroup.size();
总页码:
Integer totalPage = count % 10 == 0 ? count / 10 : count / 10 + 1;
map.put("totalPage", totalPage);
map.put("page", page);
已测试有效,希望可以帮到各位
**修改一下 :数据过多的话, if (pageNum != pageCount) 该行判断会报错,测试将!=修改为!equals---------> if (!pageNum.equals(pageCount)) **
修改:如果传入的页码大于总页码的话,会报错空指针异常,所以添加一个判断:
if (pageNum > pageCount){
return null;
}
注意:这种分页工具类只适用于少数据,对于上百万的数据,响应的时间就很长了,原因是对于集合先查出了所有的数据,然后再对所有的数据进行分页,如果上百万的数据的话,查询需要时间,所以响应很慢,更好的方法还在琢磨中,如您有更好的方法,麻烦提供一下
边栏推荐
- MySQL开窗函数
- 有了MVC,为什么还要DDD?
- 【一起学Rust】Rust的Hello Rust详细解析
- Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
- 打造基于ILRuntime热更新的组件化开发
- 限流的原理
- .NET-6.WinForm2.NanUI学习和总结
- Duplicate entry 'XXX' for key 'XXX.PRIMARY' solution.
- Unity mobile game performance optimization series: performance tuning for the CPU side
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
猜你喜欢
Puzzle Game Level Design: Reverse Method--Explaining Puzzle Game Level Design
110道 MySQL面试题及答案 (持续更新)
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
Minesweeper game (written in c language)
A complete introduction to JSqlParse of Sql parsing and conversion
MySQL常见面试题汇总(建议收藏!!!)
Centos7 install mysql5.7
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
Three oj questions on leetcode
【一起学Rust】Rust的Hello Rust详细解析
随机推荐
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
面试官竟然问我怎么分库分表?幸亏我总结了一套八股文
110 MySQL interview questions and answers (continuously updated)
城市内涝及桥洞隧道积水在线监测系统
STM32 - DMA
Unity手机游戏性能优化系列:针对CPU端的性能调优
MySQL database installation (detailed)
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
有了MVC,为什么还要DDD?
12 reasons for MySQL slow query
三道leetcode上的oj题
STM32——DMA
DVWA installation tutorial (understand what you don't understand · in detail)
打造基于ILRuntime热更新的组件化开发
SQL语句中对时间字段进行区间查询
View source and switch mirrors in two ways: npm and nrm
Visual studio shortcuts that improve efficiency, summary (updated from time to time)
The monitoring of Doris study notes
Numpy中np.meshgrid的简单用法示例
MySQL开窗函数