当前位置:网站首页>从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})
}
......
测试:
分页后的结果:
边栏推荐
- 中国矿大团队,开发集成多尺度深度学习模型,用于 RNA 甲基化位点预测
- The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds
- Working for 7 years to develop my brother's career transition test: only by running hard can you get what you want~
- useState vs useRef 和 useReducer:相同点、不同点和用例
- Test article
- Flex attribute of wechat applet
- Oracle进入sqlplus 报错
- 3 interview salary negotiation skills, easily win 2K higher than expected salary to apply for a job
- Ks008 SSM based press release system
- 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
猜你喜欢

Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?

掌舵9年,艾伦研究所创始CEO光荣退休!他曾预言中国AI将领跑世界

Error when Oracle enters sqlplus

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千

Technology creates value and teaches you how to collect wool

Thinking and Practice on Quality Standardization (suitable for product, development, testing and management post learning)

An idea plug-in for automatically generating unit tests

The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars

How to use note taking software flowus and note for interval repetition? Based on formula template

3 interview salary negotiation skills, easily win 2K higher than expected salary to apply for a job
随机推荐
2021-05-08
Working for 7 years to develop my brother's career transition test: only by running hard can you get what you want~
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of the TCC mode
Test article
Cause analysis and intelligent solution of information system row lock waiting
Technology creates value and teaches you how to collect wool
系统设计与分析课程项目个人小结
Web technology sharing | [Gaode map] to realize customized track playback
golang--文件的多个处理场景
【二级等保】过二级等保用哪个堡垒机品牌好?
2021-06-03
加快 yarn install 的三个简单技巧
【深入理解TcaplusDB技术】TcaplusDB导入数据
KS008基于SSM的新闻发布系统
渗透测试-提权专题
DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
k8s--部署单机版MySQL,并持久化
Assembly language interrupt and external device operation --06
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
Gold three silver four, busy job hopping? Don't be careless. Figure out these 12 details so that you won't be fooled~