2021-07-20 08:08:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2021-07-21 07:49:38 +00:00
|
|
|
"errors"
|
2021-07-20 08:08:12 +00:00
|
|
|
"fmt"
|
2021-07-21 07:49:38 +00:00
|
|
|
|
|
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
|
"gorm.io/gorm"
|
2021-07-20 08:08:12 +00:00
|
|
|
)
|
|
|
|
|
|
2021-07-21 07:49:38 +00:00
|
|
|
type Video struct {
|
2021-07-20 08:08:12 +00:00
|
|
|
Vimg string
|
|
|
|
|
Video string
|
2021-07-21 07:49:38 +00:00
|
|
|
Articleid int
|
|
|
|
|
// Vf gorm.DeletedAt 定义软删除
|
2021-07-20 08:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2021-07-21 07:49:38 +00:00
|
|
|
dsn := "root:123456@tcp(127.0.0.1:3306)/companyinfo?charset=utf8mb4&parseTime=True&loc=Local"
|
|
|
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
|
|
|
|
|
QueryFields: true,
|
|
|
|
|
})
|
2021-07-20 08:08:12 +00:00
|
|
|
if err != nil {
|
2021-07-21 07:49:38 +00:00
|
|
|
panic("failed to connect database")
|
2021-07-20 08:08:12 +00:00
|
|
|
}
|
2021-07-21 07:49:38 +00:00
|
|
|
// result := db.Unscoped().Where("Vimg=?", "40").Delete(&Video{})
|
|
|
|
|
// result := db.Unscoped().Where("vimg=20").Find(Video{}) //查找软删除的记录
|
|
|
|
|
// db.AutoMigrate(&Video{}) //迁移
|
|
|
|
|
// var video = Video{}
|
|
|
|
|
// video := Video{Vimg: "42", Video: "12", Articleid: 12}
|
|
|
|
|
// fmt.Println(video)
|
|
|
|
|
// db.Create(&video)
|
|
|
|
|
// result := db.First(Video{})
|
|
|
|
|
result := db.Model(Video{}).Where("vid=?", 28).Updates(map[string]interface{}{"Vimg": "40"})
|
|
|
|
|
// println(result)
|
|
|
|
|
// db.Where("vid>?", 28).Delete(Video{})
|
|
|
|
|
// var videos = Video{}
|
|
|
|
|
// db.Raw("select Vimg,Video,Articleid from videos where Vid =?", 3).Scan(Video{}) //原生sql查询 Raw Scan
|
|
|
|
|
// result := db.Where("vid=?", 28).First(&videos)
|
|
|
|
|
// result := db.Not("vid=?", 2).Find(&videos) //查询vid不等于2的
|
|
|
|
|
// result := db.Where("vid=?", 28).Or("vimg=?", 12).Find(&videos) //查询 vid=28或者vimg=12
|
|
|
|
|
// result := db.Where(map[string]interface{}{"Vimg": "41"}).Find(&videos)
|
|
|
|
|
// result := db.Order("Vimg desc,Video").Find(&videos)
|
|
|
|
|
// result := db.Limit(3).Find(&videos)
|
|
|
|
|
// result := db.Offset(1).Find(&videos)
|
|
|
|
|
// result := db.Find(&videos)
|
|
|
|
|
// db.Session(&gorm.Session{QyeryFields: true}).Find(&videos)
|
|
|
|
|
// result := db.Clauses(hints.New("MAX_EXECUTION_TIME(10000)")).Find(&Video{})
|
|
|
|
|
fmt.Println(result.RowsAffected)
|
|
|
|
|
errors.Is(result.Error, gorm.ErrRecordNotFound)
|
2021-07-20 08:08:12 +00:00
|
|
|
}
|