0 Star 0 Fork 0

shallot / Go开发工具集

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
list_by_page.go 897 Bytes
一键复制 编辑 原始数据 按行查看 历史
package dbtool
import (
"fmt"
"github.com/jinzhu/gorm"
)
// 分页查询列表的公共参数
type ListByPageParams struct {
PageNumber uint64 `json:"page_number"` // 从1开始的页码
PageSize uint64 `json:"page_size"` // 大于1为有效的页大小
Sort map[string]string `json:"sort"` // 排序信息(仅支持一个),[字段名:ascend/descend]
}
// 分页查询
func ListByPage(db *gorm.DB, params *ListByPageParams) *gorm.DB {
if params == nil {
return db
}
for k, v := range params.Sort {
if v == "ascend" {
db = db.Order(fmt.Sprintf("%s ASC", k))
} else if v == "descend" {
db = db.Order(fmt.Sprintf("%s DESC", k))
} else {
continue
}
break
}
if params.PageNumber > 0 && params.PageSize > 0 {
offset := (params.PageNumber - 1) * params.PageSize
db = db.Offset(offset).Limit(params.PageSize)
}
return db
}
Go
1
https://gitee.com/gxsshallot/gotool.git
git@gitee.com:gxsshallot/gotool.git
gxsshallot
gotool
Go开发工具集
2324c52c6c14

搜索帮助

53164aa7 5694891 3bd8fe86 5694891