SpringBoot 实现分页展示
Aidan Engineer

实现分页展示算是比较常用的功能了,几乎每一个项目都会使用到,分页查询在数据库层面的实现可以看作两种:

  • 物理查询:根据逻辑的判断,每次仅查询需要的页内数据
  • 逻辑查询:查询所有数据,根据需求显示不同的数据

这里只总结物理查询的方式

我目前接触到的物理查询的方式也是两种:

  • 手动实现 PageUtil 类,通过对参数的接收完成逻辑的处理
  • 使用 PageHelper 傻瓜式操作

PageUtil 实现

实现思路就是将本页查询需要使用到的 current, limit 等数据在类中进行逻辑的处理,然后根据前端请求的页面信息得到具体的值,在分页查询时取值即可

实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Created by Aidan on 2021/10/13 14:43
* GitHub: github.com/huaxin0304
* Blog: aidanblog.top
* @author Aidan
* 将分页处理数据进行实体封装
*/

public class Page {

/**
* 成员变量:
* current: 当前页码
* limit: 单页上限
* rows: 数据总行数
* path: 查询路径
*/
private int current = 1;
private int limit = 10;
private int rows;
private String path;

public int getCurrent() {
return current;
}

public void setCurrent(int current) {
if (current >= 1) {
this.current = current;
}
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
// 不超过 100,保证效率和阅读体验
final int maxLimit = 100;
if (limit >= 1 && limit <= maxLimit) {
this.limit = limit;
}
}

public int getRows() {
return rows;
}

public void setRows(int rows) {
if (rows >= 0) {
this.rows = rows;
}
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

/**
* 获取当前页的起始行
*
* @return 根据当前页和每页显示数计算
*/
public int getOffset() {
// current * limit - limit
return (current - 1) * limit;
}

/**
* 获取总页数
*/
public int getTotal() {
// rows / limit [+1]
if (rows % limit == 0) {
return rows / limit;
} else {
return rows / limit + 1;
}
}

/**
* 获取起始页码
*/
public int getFrom() {
int from = current - 2;
return Math.max(from, 1);
}

/**
* 获取结束页码
*/
public int getTo() {
int to = current + 2;
int total = getTotal();
return Math.min(to, total);
}
}

在前端请求的时候使用逻辑性的信息展示,在发起页面切换请求时携带分页对象的成员变量

前端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- 分页 -->
<nav class="mt-5" th:if="${page.rows>0}">
<ul class="pagination justify-content-center">
<li th:class="|page-item ${page.current==1?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=1)}">
首页
</a>
</li>
<li th:class="|page-item ${page.current==1?'disabled':''}|">
<a class="page-link" th:href="@{${page.path}(current=${(page.current)-1})}">
上一页
</a>
</li>
<li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
<!-- 使用 numbers.sequence 设置循环 -->
<a class="page-link" th:href="@{${page.path}(current=${i})}" th:text="${i}">1</a>
</li>
<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
<a class="page-link"
th:href="@{${page.path}(current=${page.current+1})}">
下一页
</a>
</li>
<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
<a class="page-link"
th:href="@{${page.path}(current=${page.total})}">
末页
</a>
</li>
</ul>
</nav>

根据需要跳转的页码和限制得出起始行,进行查询

PageHelper

PageHelper 作为一个 MyBatis 的分页插件,实现起来还是非常简单的,和其他的组件一样,这里直接贴一下 官方文档

  • 本文标题:SpringBoot 实现分页展示
  • 本文作者:Aidan
  • 创建时间:2021-11-27 12:45:03
  • 本文链接:https://aidanblog.top/springboot-pagint_display/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论