当前位置:网站首页>开源一夏 | 查询分页不只有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>
总结
边栏推荐
- 从零开始的tensorflow小白使用指北
- Producer and Consumer Problems in Concurrent Programming
- 菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
- 技术实现 | 图像检索及其在淘宝的应用
- 如何快速将Zabbix5.0升级至6.0?
- 速速脱单诀窍
- No module named 'flask_misaka' has been resolved [BUG solution]
- Detailed explanation of NAT/NAPT address translation (internal and external network communication) technology [Huawei eNSP]
- Shared_preload_libraries cause many syntaxes not supported
- Apache Druid 实时分析数据库入门介绍
猜你喜欢

DeLighT:深度和轻量化的Transformer

After four years of outsourcing, the autumn recruits finally landed

TCP的四次挥手

JSP基本语法

Apache Druid 实时分析数据库入门介绍

TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2

微信消息从发送到接收,经历了什么?如何防止丢包

学会 Arthas,让你 3 年经验掌握 5 年功力

菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
![Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]](/img/97/6c3662ef36b02bc42eec95abaa6bc5.png)
Detailed explanation of MSTP protocol configuration on Layer 3 switches [Huawei eNSP experiment]
随机推荐
Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路
软件工程国考总结——判断题
【正点原子STM32连载】第四章 STM32初体验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
区分惯性环节与延迟环节
Linux Redis cache avalanche, breakdown, penetration
优炫数据库只有数据文件如何恢复
JSP基本语法
TiDB升级与案例分享(TiDB v4.0.1 → v5.4.1)
户外徒步旅行
I am 37 this year, and I was rushed by a big factory to...
思想茶叶蛋 (Jul 31,2022)| 元宇宙(Metaverse)下了一枚什么样的蛋
一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
菲沃泰科创板上市:市值123亿 宗坚赵静艳夫妇身价76亿
Get the number of cpu cores
async - await
2022年制冷与空调设备运行操作特种作业证考试题库及模拟考试
函数防抖与函数节流
递归思想
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
