You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.1 KiB

2 years ago
package gormExample
import (
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"log"
"time"
)
type User struct {
gorm.Model
Username string
Name string
Email string
Birthday *time.Time
}
func OperatorType() {
DB.AutoMigrate(&User{})
var users []User
// 一步操作
err := DB.
Order("name DESC").
Where("email like ?", "@163.com%").
Where("birthday IS NOT NULL").
Find(&users).Error // select all
if err != nil {
log.Fatal(err)
}
// 分步操作
query := DB.Where("birthday IS NOT NULL")
query = query.Where("email like ?", "@163.com%")
query = query.Order("name DESC")
query.Find(&users)
}
func CreateBasic() {
DB.AutoMigrate(&Content{})
// 模型映射记录,操作模型字段,就是操作记录的列
c1 := Content{}
c1.Subject = "GORM的使用"
// 执行创建insert
result1 := DB.Create(&c1)
// 处理错误
if result1.Error != nil {
log.Fatal(result1.Error)
}
// 最新的ID和影响的记录数
fmt.Println(c1.ID, result1.RowsAffected)
// map 指定数据
//设置map 的values
values := map[string]any{
"Subject": "Map指定值",
"PublishTime": time.Now(),
}
// create
result2 := DB.Model(&Content{}).Create(values)
if result2.Error != nil {
log.Fatal(result2.Error)
}
// 测试输出
fmt.Println(result2.RowsAffected)
}
func CreateMulti() {
DB.AutoMigrate(&Content{})
// 定义模型的切片
models := []Content{
{Subject: "标题1"},
{Subject: "标题2"},
{Subject: "标题3"},
}
// create 插入
result := DB.Create(&models)
if result.Error != nil {
log.Fatal(result.Error)
}
fmt.Println("RowsAffected:", result.RowsAffected)
for _, m := range models {
fmt.Println("ID:", m.ID)
}
// 切片结构同样支持
vs := []map[string]any{
{"Subject": "标题4"},
{"Subject": "标题5"},
{"Subject": "标题6"},
}
result2 := DB.Model(&Content{}).Create(vs)
if result2.Error != nil {
log.Fatal(result2.Error)
}
fmt.Println("RowsAffected:", result2.RowsAffected)
}
func CreateBatch() {
DB.AutoMigrate(&Content{})
// 定义模型的切片
models := []Content{
{Subject: "标题1"},
{Subject: "标题2"},
{Subject: "标题3"},
}
// create 插入
result := DB.CreateInBatches(&models, 2)
if result.Error != nil {
log.Fatal(result.Error)
}
fmt.Println("RowsAffected:", result.RowsAffected)
for _, m := range models {
fmt.Println("ID:", m.ID)
}
// 切片结构同样支持
vs := []map[string]any{
{"Subject": "标题4"},
{"Subject": "标题5"},
{"Subject": "标题6"},
}
result2 := DB.Model(&Content{}).CreateInBatches(vs, 2)
if result2.Error != nil {
log.Fatal(result2.Error)
}
fmt.Println("RowsAffected:", result2.RowsAffected)
}
func UpSert() {
DB.AutoMigrate(&Content{})
// 常规插入,原始数据
c1 := Content{}
c1.Subject = "原始标题"
c1.Likes = 10
DB.Create(&c1)
fmt.Println(c1)
// 主键冲突的错误
//c2 := Content{}
//c2.ID = c1.ID
//c2.Subject = "新标题"
//c2.Likes = 20
//if err := DB.Create(&c2).Error; err != nil {
// log.Fatal(err)
// // Error 1062 (23000): Duplicate entry '17' for key 'msb_content.PRIMARY'
//}
// 冲突后,更新全部字段
//c3 := Content{}
//c3.ID = c1.ID
//c3.Subject = "新标题"
//c3.Likes = 20
//if err := DB.
// Clauses(clause.OnConflict{
// UpdateAll: true,
// }).
// Create(&c3).Error; err != nil {
// log.Fatal(err)
//}
// 冲突后,更新部分字段
c4 := Content{}
c4.ID = c1.ID
c4.Subject = "新标题"
c4.Likes = 20
if err := DB.
Clauses(clause.OnConflict{
DoUpdates: clause.AssignmentColumns([]string{"likes"}),
}).
Create(&c4).Error; err != nil {
log.Fatal(err)
}
}
func DefaultValue() {
DB.AutoMigrate(&Content{})
// 常规插入,原始数据
c1 := Content{}
c1.Subject = "原始标题"
c1.Likes = 0
//views := uint(0)
//c1.Views = &views
DB.Create(&c1)
//fmt.Println(c1.Likes, *c1.Views)
}
func DefaultValueOften() {
DB.AutoMigrate(&Content{})
c1 := NewContent()
c1.Subject = "原始标题"
DB.Create(&c1)
fmt.Println(c1.Likes, c1.Views)
}
func SelectOmit() {
DB.AutoMigrate(&Content{})
c1 := Content{}
c1.Subject = "原始标题"
c1.Likes = 10
c1.Views = 99
now := time.Now()
c1.PublishTime = &now
//DB.Select("Subject", "Likes", "UpdatedAt").Create(&c1)
//INSERT INTO `msb_content` (`created_at`,`updated_at`,`subject`,`likes`) VALUES ('2023-04-11 18:11:26.384','2023-04-11 18:11:26.384','原始标题',10)
DB.Omit("Subject", "Likes", "UpdatedAt").Create(&c1)
//INSERT INTO `msb_content` (`created_at`,`deleted_at`,`views`,`publish_time`) VALUES ('2023-04-11 18:12:39.431',NULL,99,'2023-04-11 18:12:39.429')
}
func CreateHook() {
DB.AutoMigrate(&Content{})
c1 := Content{}
err := DB.Create(&c1).Error
if err != nil {
log.Fatal(err)
}
//INSERT INTO `msb_content` (`created_at`,`updated_at`,`deleted_at`,`subject`,`likes`,`views`,`publish_time`) VALUES ('2023-04-11 18:44:56.62','2023-04-11 18:44:56.62',NULL,'',0,0,'2023-04-11 18:44:56.62') ON DUPLICATE KEY UPDATE `updated_at`='2023-04-11 18:44:56.62',`deleted_at`=VALUES(`deleted_at`),`subject`=VALUES(`subject`),`likes`=VALUES(`likes`),`views`=VALUES(`views`),`publish_time`=VALUES(`publish_time`)
}