阅读 206

gorm+gin实现restful分页接口的实践

本文主要介绍了gorm+gin实现restful分页接口的实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

目录
  • 1. 定义分页struct

  • 2. 数据表Model

  • 3. 定义分页查询搜索的结构体

  • 4. 分页和搜索数据查询

  • 5.例子代码

API处理分页看似简单,实际上暗藏危机.最常见的分页方式,大概是下面这样的

  • 页数表示法:/user/?page=1&size=15&name=李

  • 偏移量表示法:/user/?offset=100&limit=15&name=李

使用页码表示法对前端开发比较友好,但是本质上是和偏移量表示发相似. 在这里我们将使用 jinzhu/gorm和 gin-gonic/gin 开发一个简单的分页接口

分页查询URL: http://dev.mojotv.cn:3333/api/ssh-log?client_ip=&page=1&size=10&user_id=0&machine_id=0 返回json 结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    "data": [
        {
            "id": 28,
            "created_at": "2019-09-12T14:25:54+08:00",
            "updated_at": "2019-09-12T14:25:54+08:00",
            "user_id": 26,
            "machine_id": 1,
            "ssh_user": "mojotv.cn",
            "client_ip": "10.18.60.16",
            "started_at": "2019-09-12T14:24:05+08:00",
            "status": 0,
            "remark": ""
        }
    ],
    "ok": true,
    "page": 1,
    "size": 10,
    "total": 1
}

1. 定义分页struct

1
2
3
4
5
6
7
8
//PaginationQ gin handler query binding struct
type PaginationQ struct {
 Ok    bool        `json:"ok"`
 Size  uint        `form:"size" json:"size"`
 Page  uint        `form:"page" json:"page"`
 Data  interface{} `json:"data" comment:"muster be a pointer of slice gorm.Model"` // save pagination list
 Total uint        `json:"total"`
}
  • Ok 代表业务查询没有出错

  • Size 每页显示的数量,使用 form tag 接受gin的url-query参数

  • Page 当前页码,使用 form tag 接受gin的url-query参数

  • Data 分页的数据内容

  • Total 全部的页码数量

2. 数据表Model

这里以ssh_log(ssh 命令日志为示例),使用GORM创建MYSQL数据表模型, 使用 form tag 接受gin的url-query参数,作为搜索条件

1
2
3
4
5
6
7
8
9
10
11
12
13
type SshLog struct {
 BaseModel
 UserId    uint      `gorm:"index" json:"user_id" form:"user_id"` //form tag 绑定gin url-query 参数
 MachineId uint      `gorm:"index" json:"machine_id" form:"machine_id"` //form tag 绑定gin url-query 参数
 SshUser   string    `json:"ssh_user" comment:"ssh账号"`
 ClientIp  string    `json:"client_ip" form:"client_ip"` //form tag 绑定gin url-query 参数
 StartedAt time.Time `json:"started_at" form:"started_at"`
 Status    uint      `json:"status" comment:"0-未标记 2-正常 4-警告 8-危险 16-致命"`
 Remark    string    `json:"remark"`
 Log       string    `gorm:"type:text" json:"log"`
 Machine   Machine   `gorm:"association_autoupdate:false;association_autocreate:false" json:"machine"`
 User      User      `gorm:"association_autoupdate:false;association_autocreate:false" json:"user"`
}

3. 定义分页查询搜索的结构体

1
2
3
4
5
6
7
ssh2ws/internal/h_ssh_log.go
type SshLogQ struct {
 SshLog
 PaginationQ
 FromTime string `form:"from_time"` //搜索开始时间
 ToTime   string `form:"to_time"`  //搜索结束时候
}

这个结构体是提供给gin handler用作参数绑定的. 使用的方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
func SshLogAll(c *gin.Context) {
 query := &model.SshLogQ{}
 err := c.ShouldBindQuery(query) //开始绑定url-query 参数到结构体
 if handleError(c, err) {
  return
 }
 list, total, err := query.Search()  //开始mysql 业务搜索查询
 if handleError(c, err) {
  return
 }
 //返回数据开始拼装分页json
 jsonPagination(c, list, total, &query.PaginationQ)
}

4. 分页和搜索数据查询

1.创建 db-query
2.搜索非空业务字段
3.使用crudAll 方法获取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
model/m_ssh_log.go
type SshLogQ struct {
 SshLog
 PaginationQ
 FromTime string `form:"from_time"`
 ToTime   string `form:"to_time"`
}
 
func (m SshLogQ) Search() (list *[]SshLog, total uint, err error) {
 list = &[]SshLog{}
 //创建 db-query
 tx := db.Model(m.SshLog).Preload("User").Preload("Machine")
 //搜索非空业务字段
 if m.ClientIp != "" {
  tx = tx.Where("client_ip like ?", "%"+m.ClientIp+"%")
 }
 //搜索时间段
 if m.FromTime != "" && m.ToTime != "" {
  tx = tx.Where("`created_at` BETWEEN ? AND ?", m.FromTime, m.ToTime)
 }
 //使用crudAll 方法获取数据
 total, err = crudAll(&m.PaginationQ, tx, list)
 return
}

crudAll 方法来构建sql分页数据,

  • 设置默认参数

  • 获取全部搜索数量

  • 获取偏移量的数据

  • 拼装json 分页数据 

model/helper.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
func crudAll(p *PaginationQ, queryTx *gorm.DB, list interface{}) (uint, error) {
    //1.默认参数
    if p.Size < 1 {
        p.Size = 10
    }
    if p.Page < 1 {
        p.Page = 1
    }
 
    //2.部搜索数量
    var total uint err := queryTx.Count(&total).Error if err != nil {
        return 0, err
    }
    offset := p.Size * (p.Page - 1)
 
    //3.偏移量的数据
    err = queryTx.Limit(p.Size).Offset(offset).Find(list).Error if err != nil {
        return 0, err
    }
     
    return total, err
}
 
//4.json 分页数据
func jsonPagination(c *gin.Context, list interface{}, total uint, query *model.PaginationQ) {
    c.AbortWithStatusJSON(200, gin.H{
        “ok”: true,
        “data”: list,
        “total”: total,
        “page”: query.Page,
        “size”: query.Size
    })
 }

API处理分页看似简单,实际上暗藏危机.最常见的分页方式,大概是下面这样的

  • 页数表示法:/user/?page=1&size=15&name=李

  • 偏移量表示法:/user/?offset=100&limit=15&name=李

使用页码表示法对前端开发比较友好,但是本质上是和偏移量表示发相似. 在这里我们将使用 jinzhu/gorm和 gin-gonic/gin 开发一个简单的分页接口

分页查询URL: http://dev.mojotv.cn:3333/api/ssh-log?client_ip=&page=1&size=10&user_id=0&machine_id=0 返回json 结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    "data": [
        {
            "id": 28,
            "created_at": "2019-09-12T14:25:54+08:00",
            "updated_at": "2019-09-12T14:25:54+08:00",
            "user_id": 26,
            "machine_id": 1,
            "ssh_user": "mojotv.cn",
            "client_ip": "10.18.60.16",
            "started_at": "2019-09-12T14:24:05+08:00",
            "status": 0,
            "remark": ""
        }
    ],
    "ok": true,
    "page": 1,
    "size": 10,
    "total": 1
}

5.例子代码

完整项目代码地址

到此这篇关于gorm+gin实现restful分页接口的实践的文章就介绍到这了

原文链接:https://juejin.cn/post/7026896746068312072


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐