首先需要創建一個結構體和數據庫綁定
假設結構體長這樣
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)
大功告成