我使用的是PostgreSQL数据库,就以它为例展示如何完成后台数据分页,还好是数据库本身带有的特性。
数据库文章表里原始数据:
csdn=# select * from articles order by updated_at desc;
id | created_at | updated_at | deleted_at | title
| content | category
| tag | author
----+-------------------------------+-------------------------------+-------------------------------+-----------------------------------------
------------------+------------------------------------------------------------------------------------------------------+--------------------
-------+------------------------------+----------------------------------------------
6 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:46:40.626837+08 | 测试删除
| 测试删除 | ['golang','postgrxy
esql'] | ['golang','go'] | ['dongguangming', 'dgm']
5 | 2020-07-16 13:09:47.795349+08 | 2020-07-16 13:09:47.795349+08 | | 我在江宁
| 江宁地广,路很宽 | ['南京','江宁']
| ['南京','百家湖','购物'] | ['dmg']
4 | 2020-07-16 13:08:35.71687+08 | 2020-07-16 13:08:35.71687+08 | | 新街口
| 新街口商场很多,东西也贵 | ['南京','金陵']
| ['南京','金陵','购物'] | ['dmg','董广明']
3 | 2020-07-16 13:07:18.856783+08 | 2020-07-16 13:07:18.856783+08 | | 南京总统府咋样
| 南京总统府很好玩 | ['南京','金陵']
| ['南京','金陵','旅游'] | ['董广明']
2 | 2020-07-16 13:05:56.92499+08 | 2020-07-16 13:05:56.92499+08 | | Java开发RESTful API
| java开发接口开发过程 | ['java','postgresql
'] | ['java','code','postgresql'] | ['dongguangming', '董广明']
1 | 2020-07-16 12:57:17.621445+08 | 2020-07-16 12:57:17.621445+08 | | Golang学习系列第五天: Golang和PostgreSQ
L开发 RESTful API | 记录接口开发过程,出了问题,直接看博客,不,需要再查其他网文,节省时间干其他事,时间就是毛爷爷!!! | ['golang','postgrxy
esql'] | ['golang','go','postgresql'] | ['dongguangming', 'dgm','董广明','知名学者']
(6 rows)
通过limit和offset控制返回的数据行
csdn=# select * from articles order by updated_at desc limit 2;
id | created_at | updated_at | deleted_at | title | content | ca
tegory | tag | author
----+-------------------------------+-------------------------------+-------------------------------+----------+------------------+-----------
----------------+--------------------------+--------------------------
6 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:46:40.626837+08 | 测试删除 | 测试删除 | ['golang',
'postgrxyesql'] | ['golang','go'] | ['dongguangming', 'dgm']
5 | 2020-07-16 13:09:47.795349+08 | 2020-07-16 13:09:47.795349+08 | | 我在江宁 | 江宁地广,路很宽 | ['南京','
宁'] | ['南京','百家湖','购物'] | ['dmg']
(2 rows)
csdn=# select * from articles order by updated_at desc limit 2 offset 0;
id | created_at | updated_at | deleted_at | title | content | ca
tegory | tag | author
----+-------------------------------+-------------------------------+-------------------------------+----------+------------------+-----------
----------------+--------------------------+--------------------------
6 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:42:20.291723+08 | 2020-07-16 13:46:40.626837+08 | 测试删除 | 测试删除 | ['golang',
'postgrxyesql'] | ['golang','go'] | ['dongguangming', 'dgm']
5 | 2020-07-16 13:09:47.795349+08 | 2020-07-16 13:09:47.795349+08 | | 我在江宁 | 江宁地广,路很宽 | ['南京','
宁'] | ['南京','百家湖','购物'] | ['dmg']
(2 rows)
csdn=# select * from articles order by updated_at desc limit 2 offset 2;
id | created_at | updated_at | deleted_at | title | content | category
| tag | author
----+-------------------------------+-------------------------------+------------+----------------+--------------------------+----------------
-+------------------------+------------------
4 | 2020-07-16 13:08:35.71687+08 | 2020-07-16 13:08:35.71687+08 | | 新街口 | 新街口商场很多,东西也贵 | ['南京','金陵']
| ['南京','金陵','购物'] | ['dmg','董广明']
3 | 2020-07-16 13:07:18.856783+08 | 2020-07-16 13:07:18.856783+08 | | 南京总统府咋样 | 南京总统府很好玩 | ['南京','金陵']
| ['南京','金陵','旅游'] | ['董广明']
(2 rows)
csdn=# select * from articles order by updated_at desc limit 2 offset 4;
id | created_at | updated_at | deleted_at | title |
content | category |
tag | author
----+-------------------------------+-------------------------------+------------+-----------------------------------------------------------+
------------------------------------------------------------------------------------------------------+---------------------------+-----------
-------------------+----------------------------------------------
2 | 2020-07-16 13:05:56.92499+08 | 2020-07-16 13:05:56.92499+08 | | Java开发RESTful API |
java开发接口开发过程 | ['java','postgresql'] | ['java','c
ode','postgresql'] | ['dongguangming', '董广明']
1 | 2020-07-16 12:57:17.621445+08 | 2020-07-16 12:57:17.621445+08 | | Golang学习系列第五天: Golang和PostgreSQL开发 RESTful API |
记录接口开发过程,出了问题,直接看博客,不,需要再查其他网文,节省时间干其他事,时间就是毛爷爷!!! | ['golang','postgrxyesql'] | ['golang',
'go','postgresql'] | ['dongguangming', 'dgm','董广明','知名学者']
(2 rows)
csdn=# select * from articles order by updated_at desc limit 2 offset 6;
id | created_at | updated_at | deleted_at | title | content | category | tag | author
----+------------+------------+------------+-------+---------+----------+-----+--------
(0 rows)
csdn=#