gorm之结构体数组查询多个


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

假设结构体长这样

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)

大功告成


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM