dao.go 820 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
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 34 35 36 37 38 39
package dao

import (
	"gomicro-base/common/mysql"
	"gomicro-base/service/demo/internal/conf"
	"gomicro-base/service/demo/internal/model"
	"gorm.io/gorm"
)

// Dao 查询数据库
type Dao struct {
	DB *gorm.DB
}

func New(c *conf.Config) (d *Dao) {
	d = &Dao{
		DB: mysql.NewDB(c.Mysql),
	}
	return
}

// GetUserById 根据id查询用户
func (d *Dao) GetUserById(id int, column []string) (res *model.User, err error) {
	res = &model.User{}
	err = d.DB.Model(&model.User{}).
		Select(column).
		Where("id = ?", id).Take(res).Error
	return
}

// ListUserGtId 查询大于该id的用户列表
func (d *Dao) ListUserGtId(id int, limit int) (res []*model.User, err error) {
	res = make([]*model.User, 0)
	err = d.DB.Model(&model.User{}).
		Where("id > ?", id).
		Limit(limit).Order("id desc").
		Find(&res).Error
	return
}