var total int
page,_ := strconv.Atoi(c.DefaultPostForm("page","1"))
pageSize,_ := strconv.Atoi(c.DefaultPostForm("pageSize","10"))
//此处用了PostForm的请求方法
db = models.db.Model(Student{}) //查询对应的数据库表
//这里的models是对数据库进行初始化以及Gorm中的model结构体定义,如下:
/*
var db *gorm.DB
type Model struct {
ID int `gorm:"primary_key" json:"id"`
CreatedOn int `json:"-"`
ModifiedOn int `json:"-"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
}*/
if err := db.Count(&total).Error; err != nil{
c.JSON(http.StatusOK, gin.H{
"code" : 500,
"message" : "查询数据异常",
})
return
}
//此时的total是查询到的总数
offset := (page-1)*pageSize
if err := db.Order("id DESC").offset(offset).Limit(pageSize).Find(&data).Error;err != nil{
c.JSON(http.StatusOK, gin.H{
"code" : 500,
"message" : "查询数据异常",
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 200,
"message": "success",
"data" : map[string]interface{}{
"data" : data,
"total": total,
"page" : page,
"pageSize": pageSize,
},
})
return
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)