分页查询
Page()
分页查询数据,需要传两个参数,第几页和每页多少条
- @param
current
第几页 - @param
size
每页显示多少条
Page()
var user []User
err := tg.Db("user").Page(1, 10).Select(&user)
var user []User
err := tg.Db("user").Page(1, 10).Select(&user)
最终的SQL语句为:
SELECT * FROM user LIMIT 0, 10
SELECT * FROM user LIMIT 0, 10
常用技巧
func UserList(req api.UserListReq) (res api.UserListRes, err error) {
m := tg.Db("User")
if req.PageNum == 0 {
req.PageNum = 1
}
if req.PageSize == 0 {
req.PageSize = 10
}
if req.Id != 0 {
m = m.Where("id", "=", req.Id)
}
var user []entity.User
count, err := m.Count()
err = m.Page(req.PageNum, req.PageSize).Select(&user)
res.List = user
res.Total = count
return
}
func UserList(req api.UserListReq) (res api.UserListRes, err error) {
m := tg.Db("User")
if req.PageNum == 0 {
req.PageNum = 1
}
if req.PageSize == 0 {
req.PageSize = 10
}
if req.Id != 0 {
m = m.Where("id", "=", req.Id)
}
var user []entity.User
count, err := m.Count()
err = m.Page(req.PageNum, req.PageSize).Select(&user)
res.List = user
res.Total = count
return
}