当前位置:网站首页>开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
2022-08-04 09:06:00 【InfoQ】
- 由于前端在页面显示数据时,希望每个页面只显示指定条数的数据,由于数据量较大,不能够一次性展示,所以就通过限制每个页面固定的条数进行展示。
- 如果数据量较大的话,页面也不一定加载完全,所以造成效率比较差,所以解决方案分页查询就可以很好的解决这个问题了,通过把庞大的数据按照固定的数目显示,在通过上一页或下一页的按钮显示其它的数据。
limit的传统方式
- 如果是一个参数,如limit 10,则表示默认从开始位置开始获取10条数据。
- 如果是两个参数,第一个参数指定的是返回行数的开始位置,第二个参数指定的是返回记录行的数目。Eg: select * from user limit 0,10表示从第0个位置开始获取10条数据。
PageHelper插件的方式
- 引入依赖
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
- 使用代码注意
PageHelper.startPage(pageNo, pageSize)
PageResult<List<Employee>> result = new PageResult<>();
//就是下边的这个地方,使用了 pageNo是offset就是开始位置的偏移量,第二个参数pageSize是查询返回的数据的条数
PageHelper.startPage(pageNo, pageSize);
try {
List<Employee> employeeList = employeeService.findAll();
PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
log.info(String.valueOf(employeeList));
result.setTotal(pageInfo.getTotal());
result.setPages(pageInfo.getPages());
result.setCurrent(pageInfo.getPageNum());
result.setSize(pageInfo.getPageSize());
result.setResult(employeeList);
result.success("查询成功!");
}
- 查询结果
自定义Interceptor的方式
RowsBounds方式
service层:
RowBounds rowbounds = new RowBounds(offset, Integer.parseInt(pageSize));
//用户数据集合
List<Map<String, Object>> userList = userDao.queryUserList(rowbounds);
Dao层:
public List<Map<String, Object>> queryUserList(RowBounds rowbounds);
mapperxml层:
<!-- 查询用户列表 -->
<select id="queryUserList" resultType="java.util.Map">
select * from user
</select>
总结
边栏推荐
- 他97年的,我既然卷不过他...
- It is found that several WRH tables are locked, what should I do?
- ansible部署脚本--亲测可用无坑
- 【正点原子STM32连载】第一章 本书学习方法 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
- [Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
- 【高并发基石】多线程、守护线程、线程安全、线程同步、互斥锁
- Fiddler(一)安装
- 2022-08-02 分析RK817 输出32k clock PMIC_32KOUT_WIFI给WiFi模块 clock 注册devm_clk_hw_register
- 交换机链路聚合详解【华为eNSP】
- v-model原理,在“radio”、“checkbox”、“select”、修饰符
猜你喜欢
MindSpore:Batchnorm only support nchw input!
有坦荡的远方
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
学会 Arthas,让你 3 年经验掌握 5 年功力
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
从零开始的tensorflow小白使用指北
Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
MindSpore:model.train中的dataset_sink_mode该如何理解?
基于cRIO-904X搭建Simulink与Labview环境
recursive thinking
随机推荐
oracle sql 多表查询
他97年的,我既然卷不过他...
张朝阳对话俞敏洪:谈宇宙、谈焦虑、谈创业、谈退休、谈人生
【Attention】Dual Attention(DANet) & Fully Attention(FLA)
Apache Druid 实时分析数据库入门介绍
速速脱单诀窍
After four years of outsourcing, the autumn recruits finally landed
v-model原理,在“radio”、“checkbox”、“select”、修饰符
王爽汇编语言第四章:第一个程序
户外徒步旅行
leetcode二叉树系列(二叉搜索树篇)
Shared_preload_libraries导致很多语法不支持
cannot import name 'import_string' from 'werkzeug' [bug solution]
今年37了,被大厂抢着要...
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此
LVGL的多语言转换工具--字体设置的好助手
【论文笔记】Understanding Long Programming Languages with Structure-Aware Sparse Attention
Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
蘑菇书EasyRL学习笔记