1 Star 0 Fork 0

any-call / mygin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
base.go 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
jinhua 提交于 2024-01-26 08:54 . changed
package mygin
import (
"gorm.io/gorm"
)
type (
ID struct {
ID int `json:"id" form:"id" validate:"min(1,入参ID不能为空)"`
}
IDs struct {
IDs []int `json:"ids" form:"ids" validate:"minlength(1,入参ID不能为空)"`
}
PageReq struct {
Limit int `form:"limit" validate:"min(1,无效的分页数据)"`
Page int `form:"page"`
}
PageResp[T any] struct {
Total int64 `json:"total"`
Page int `json:"page"`
Limit int `json:"limit"`
List []T `json:"list"`
}
)
func Pagination[T any](db *gorm.DB, req PageReq, resp *PageResp[T]) (err error) {
if err = db.Count(&(resp.Total)).Error; err != nil {
return
}
if resp.Total == 0 {
return
}
if req.Limit <= 0 {
req.Limit = 10
}
if req.Page <= 0 {
req.Page = 1
}
resp.Page = req.Page
resp.Limit = req.Limit
err = db.Offset(req.Limit * (req.Page - 1)).Limit(req.Limit).Find(&(resp.List)).Error
return
}
func Pagination_other(db *gorm.DB, limit, page int, count *int64, list any) (err error) {
if err = db.Count(count).Error; err != nil {
return
}
if *count == 0 {
return
}
if limit <= 0 {
limit = 10
}
if page <= 0 {
page = 1
}
err = db.Offset(limit * (page - 1)).Limit(limit).Find(list).Error
return
}
Go
1
https://gitee.com/any-call/mygin.git
git@gitee.com:any-call/mygin.git
any-call
mygin
mygin
v1.1.5

搜索帮助