演示样例
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// 1. 定义模型
type User struct{
gorm.Model
Name string
Age int
Active bool
}
func main(){
// 2. 连接数据库
db , err := gorm.Open("mysql",
"root:root@(127.0.0.1:3306)/db1?charset=utf8&mb4&parseTime=True&loc=Local")
if err != nil{
fmt.Printf("connect DB failed , err : %v\n",err)
return
}
defer db.Close()
fmt.Printf("连接数据库成功!\n")
// 3. 数据库的表与模型建立关系
db.AutoMigrate(User{})
// 4. 创建数据添加到库中
db.Create(&User{Name:"Crazy", Age:12, Active:false})
db.Create(&User{Name:"LiMei", Age:16, Active:true})
db.Create(&User{Name:"GeLei", Age:20, Active:false})
db.Create(&User{Name:"Will", Age:30, Active:true})
}
更新 字段
// 6. 更新 - save 必须要获取主键,才能更新对应的行,不然就会生成一条新的创建到表内;
// 更新所有字段
// UPDATE `users` SET `created_at` = '2021-03-09 09:58:27', `updated_at` = '2021-03-09 09:58:27', `deleted_at` = NULL, `name` = 'Ali', `age` = 19, `active` = true WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 4
db.Debug().Save( &user )
user = User{}
// 更新修改字段 (根据主键 确定哪一行)
// 如果你只希望更新指定字段,可以使用Update或者Updates
// UPDATE `users` SET `name` = 'hello', `updated_at` = '2021-03-09 09:58:27' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model( &user ).Update("name", "hello1")
// 根据给定的条件更新单个属性
// UPDATE `users` SET `name` = 'hello2', `updated_at` = '2021-03-09 10:07:38' WHERE `users`.`deleted_at` IS NULL AND ((active = true))
db.Debug().Model( &user ).Where("active = ?", true).Update("name", "hello2")
// 使用 map 更新多个属性,只会更新其中有变化的属性
// UPDATE `users` SET `active` = false, `age` = 18, `name` = 'hello3', `updated_at` = '2021-03-09 10:14:26' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model( &user ).Updates(map[string]interface{}{"name": "hello3", "age": 18, "active": false})
// 使用 struct 更新多个属性,只会更新其中有变化且为非零值的字段
// UPDATE `users` SET `age` = 18, `name` = 'hello4', `updated_at` = '2021-03-09 10:15:34' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model( &user ).Updates(User{Name: "hello4", Age: 18})
// 警告:当使用 struct 更新时,GORM只会更新那些非零值的字段
// 对于下面的操作,不会发生任何更新,"", 0, false 都是其类型的零值
// 不执行
db.Debug().Model( &user ).Updates(User{Name: "", Age: 0, Active: false})
// 6. 更新
// 更新选定字段
user = User{}
// UPDATE `users` SET `name` = 'hello5', `updated_at` = '2021-03-09 10:31:35' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).Select("name").Updates(map[string]interface{}{"name": "hello5", "age": 18, "active": false})
// UPDATE `users` SET `active` = false, `age` = 22, `updated_at` = '2021-03-09 10:36:55' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).Omit("name").Updates(map[string]interface{}{"name": "hello6", "age": 22, "active": false})
// 无Hooks更新
// 更新单个属性,类似于 `Update`
// UPDATE `users` SET `name` = 'hello7' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).UpdateColumn("name", "hello7")
// 更新多个属性,类似于 `Updates`
// UPDATE `users` SET `age` = 18, `name` = 'hello8' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).UpdateColumns(User{Name: "hello8", Age: 18})
// UPDATE `users` SET `age` = 18, `name` = 'hello9' WHERE (id IN (3,4))
db.Debug().Table("users").Where("id IN (?)", []int{3, 4}).Updates(map[string]interface{}{"name": "hello9", "age": 18})
//// UPDATE users SET name='hello', age=18 WHERE id IN (10, 11);
// 使用 struct 更新时,只会更新非零值字段,若想更新所有字段,请使用map[string]interface{}
// UPDATE `users` SET `age` = 18, `name` = 'hello10', `updated_at` = '2021-03-09 10:40:34' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(User{}).Updates(User{Name: "hello10", Age: 18})
// 使用 `RowsAffected` 获取更新记录总数
// UPDATE `users` SET `age` = 18, `name` = 'hello11', `updated_at` = '2021-03-09 10:41:16' WHERE `users`.`deleted_at` IS NULL
// rows = 4
rows := db.Debug().Model(User{}).Updates(User{Name: "hello11", Age: 18}).RowsAffected
fmt.Printf("%d\n",rows)
// UPDATE `users` SET `age` = age * 2 + 100, `updated_at` = '2021-03-09 10:42:14' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).Update("age", gorm.Expr("age * ? + ?", 2, 100))
// UPDATE `users` SET `age` = age * 2 + 100, `updated_at` = '2021-03-09 10:42:14' WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).Updates(map[string]interface{}{"age": gorm.Expr("age * ? + ?", 2, 100)})
// UPDATE `users` SET `age` = age - 1 WHERE `users`.`deleted_at` IS NULL
db.Debug().Model(&user).UpdateColumn("age", gorm.Expr("age - ?", 1))
// UPDATE `users` SET `age` = age - 1 WHERE `users`.`deleted_at` IS NULL AND ((age > 10))
db.Debug().Model(&user).Where("age > 10").UpdateColumn("age", gorm.Expr("age - ?", 1))