goorm之结构体数组查询多个

首先需要创建一个结构体和数据库绑定

假设结构体长这样

type Topic struct {
ID uint
TheTopic string
Hash string
TopicType int
Answer string
Correct string
CreateTime int64
CreateIp string
AllHash string `gorm:"PRIMARY_KEY"`
CourseName string
}

则在数据库中是这样,表名topics

其中有一点特别要注意 :单词开头要大写!单词开头要大写!单词开头要大写!

通过 AutoMigrate() 这个方法自动创建表

接着是查询:

var topics []Topic

看到中括号了吗,定义结构体数组

查询时记得传地址

db.Where(`id > ?`,xxx).Find(&topics)

大功告成

猜你喜欢

转载自www.cnblogs.com/lyj0309/p/12953635.html