当前位置:网站首页>Starting from 3, add paging function in the business system
Starting from 3, add paging function in the business system
2022-06-23 15:03:00 【Barry Yan】
This section is relatively simple , The implementation of adding paging function makes use of gorm And synchronized changes Gin Receiving parameters of , And then realize the paging function .
Design logic :
SQL grammar SELECT * FROM table LIMIT 10 OFFSET 5 , Just translate it from 5 Began to take 10 Data ,OFFSET It can be understood as skipping these lines and fetching data from behind ,LIMIT Just take a few lines of data .
So we have gorm You can use the encapsulated API, Such as Limit() and Offset() etc. , But you need two parameters , One is page Stands for page , The other is limit How many pieces of data per page , Next, write the code .
Let's take a look at the modified code :
dao Layer implementation method :
......
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 Layer interface :
......
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})
}
......
test :
Results after paging :
边栏推荐
- 基因检测,如何帮助患者对抗疾病?
- 详解Redis分布式锁的原理与实现
- 狂奔的极兔,摔了一跤
- Force deduction solution summary 513- find the value of the lower left corner of the tree
- 【深入理解TcaplusDB技术】TcaplusDB导入数据
- 分布式数据库使用逻辑卷管理存储之扩容
- What do you mean by waiting for insurance records? Where should I go for filing?
- RF analyzer demo setup
- MySQL高级语句二
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of the TCC mode
猜你喜欢

Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?

2021-05-22

2021-05-08

Why is Xiaomi stuck in the chip quagmire?

The well-known face search engine provokes public anger: just one photo will strip you of your pants in a few seconds

2021-04-15

How can genetic testing help patients fight disease?

大厂架构师:如何画一张大气的业务大图?

这届文娱人,将副业做成了主业

SFOD:无源域适配升级优化,让检测模型更容易适应新数据(附论文下载)
随机推荐
加快 yarn install 的三个简单技巧
MySQL高级语句一
ACM Player Illustration leetcode remove element
Résumé de la méthode de déduction de la force 513 - trouver la valeur du coin inférieur gauche de l'arbre
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
乐高宣布涨价,炒家更嗨皮了
MySQL advanced statement 2
mysql主从只同步部分库或表的思路与方法
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
2021-06-07
Execute the sc.exe QC command to query some services. The data area passed to the system call is too small
Use of pyqt5 tool box
raspberry pi安装 wiringpi
What is the charm of Guizhou? Why do Alibaba, Huawei and Tencent build data centers in Guizhou?
2021-05-08
用OBS做直播推流简易教程
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
k8s--部署单机版MySQL,并持久化
SQL injection vulnerability (principle)
中国矿大团队,开发集成多尺度深度学习模型,用于 RNA 甲基化位点预测