当前位置:网站首页>从3开始,在业务系统中增加分页功能
从3开始,在业务系统中增加分页功能
2022-06-23 14:00:00 【Barry Yan】
这一节相对比较的简单,增加分页功能的实现利用了gorm的封装的方法并且同步更改了Gin的接收参数,进而实现了分页功能。
设计逻辑:
SQL语法SELECT * FROM table LIMIT 10 OFFSET 5 ,翻译过来就行从5开始取10条数据,OFFSET可以理解成跳过这几行从后面开始取数据,LIMIT就是取几行数据。
因此我们在gorm中就可以利用封装好的API,如Limit()和Offset()等,但是需要两个参数,一个就是page代表第几页,另一个则是limit代表每页多少条数据,接下来写代码。
下面我们看下修改后的代码:
dao层实现方法:
......
func (impl CountNumDAOImpl) FindAllNumInfo(ctx context.Context, page int, limit int) []entity.NumInfo {
var infos []entity.NumInfo
if page <= 0 || limit <= 0 {
impl.db.Find(&infos)
} else {
impl.db.Limit(limit).Offset((page - 1) * limit).Find(&infos)
}
return infos
}
......
controller层接口:
......
func (impl NumInfoControllerImpl) FindAll(c *gin.Context) {
page := c.Query("page")
limit := c.Query("limit")
numInfos := impl.dao.FindAllNumInfo(c, cast.ToInt(page), cast.ToInt(limit))
c.JSON(200, map[string]interface{
}{
"code": 0, "msg": "", "count": len(numInfos), "data": numInfos})
}
......
测试:
分页后的结果:
边栏推荐
- Do you know which position in the IT industry has the most girls?
- KS003基于JSP和Servlet实现的商城系统
- Problems during MySQL uninstallation
- [digital signal processing] linear time invariant system LTI (judge whether a system is a "non time variant" system | case 2)
- Introduction to helm basics helm introduction and installation
- 【深入理解TcaplusDB技术】单据受理之表管理
- 去 OPPO 面试, 被问麻了。。。
- 知名人脸搜索引擎惹众怒:仅需一张照片,几秒钟把你扒得底裤不剩
- 2021-06-07
- 2021-04-15
猜你喜欢

Ks007 realizes personal blog system based on JSP
![[digital signal processing] linear time invariant system LTI (judge whether a system is a](/img/98/6819646ea602781101ac9994213129.jpg)
[digital signal processing] linear time invariant system LTI (judge whether a system is a "non time variant" system | case 3)

加快 yarn install 的三个简单技巧

去 OPPO 面试, 被问麻了。。。

Unity realizes the function of playing Ogg format video

巴比特 | 元宇宙每日必读:Meta、微软等科技巨头成立元宇宙标准论坛组织,华为、阿里加入,英伟达高管称欢迎来自加密世界的参与者...

【深入理解TcaplusDB技术】单据受理之表管理

useState vs useRef 和 useReducer:相同点、不同点和用例

Babbitt | metauniverse daily must read: meta, Microsoft and other technology giants set up the metauniverse Standards Forum. Huawei and Alibaba joined. NVIDIA executives said that they welcomed partic

In this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
随机推荐
期货怎么开户安全吗,期货手续费哪家期货公司比较低,适合散户开户?
KS007基于JSP实现人个人博客系统
Problems during MySQL uninstallation
Thinking and Practice on Quality Standardization (suitable for product, development, testing and management post learning)
信贷产品额度定价场景下的回归模型效果评估
Test article
[deeply understand tcapulusdb technology] table management of document acceptance
【二级等保】过二级等保用哪个堡垒机品牌好?
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Test article
知名人脸搜索引擎惹众怒:仅需一张照片,几秒钟把你扒得底裤不剩
How to use note taking software flowus and note for interval repetition? Based on formula template
ts封装请求
系统设计与分析-技术报告-定时清理验证码的一种解决方案
谷歌&HuggingFace| 零样本能力最强的语言模型结构
Unity realizes the function of playing Ogg format video
[deeply understand tcapulusdb technology] tmonitor module architecture
Pyqt5 工具盒使用
JSP_ SSM Wanfu cinema ticket sales management system, including paper [demonstration video]
High quality coding - air quality map visualization