定义用户结构体和地址结构体
type User struct {
gorm.Model
Name string
Age int
Active bool
Addresss []Address
}
type Address struct {
gorm.Model
Name string
Code string
UserID uint
}
初始化数据库
var db *gorm.DB
func init() {
dsn := "root:root@tcp(127.0.0.1:3306)/go_orm?charset=utf8mb4&parseTime=True&loc=Local"
d, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
if err != nil {
log.Fatal(err)
}
db = d
db.AutoMigrate(&User{}, &Address{})
}
创建模拟数据
// 批量插入用户数据
func insertManyUser() {
var users = []User{
{Name: "1", Age: 1}, {Name: "2", Age: 2},
{Name: "3", Age: 3}, {Name: "4", Age: 4},
}
d := db.Select("Name", "Age").Create(&users)
fmt.Printf("d.RowsAffected: %v\n", d.RowsAffected)
}
// 批量插入用户地址数据
func insertManyAddress() {
var addresss = []Address{
{Name: "1-1", Code: "code1", UserID: 1},
{Name: "1-2", Code: "code2", UserID: 1},
{Name: "2-1", Code: "code1", UserID: 2},
{Name: "2-2", Code: "code2", UserID: 2},
{Name: "2-3", Code: "code3", UserID: 2},
{Name: "3-2", Code: "code1", UserID: 3},
}
d := db.Create(&addresss)
fmt.Printf("d.RowsAffected: %v\n", d.RowsAffected)
}
初始化模拟数据运行结果
查询单个人信息以及单个人下面地址信息加条件
func test() {
var user User
db.Model(&User{}).Where("id", 2).Find(&user)
var addresss []Address
// `user` 是源模型,它的主键不能为空
// 关系的字段名是 `Addresss`
db.Model(&user).Where("code IN ?", []string{"code1", "code3"}).Association("Addresss").Find(&addresss)
user.Addresss = addresss
fmt.Printf("user: %+v\n", user)
}
添加关联
为单个人添加一个地址信息或添加多个信息
func test() {
// 为用户1添加地址1-3 code3
var user1 User
db.Model(&User{}).Where("id", 1).Find(&user1)
db.Model(&user1).Association("Addresss").Append(&Address{Name: "1-3", Code: "code3"})
// 为用户3添加地址3-1 code1 3-3 code3
var user3 User
db.Model(&User{}).Where("id", 3).Find(&user3)
address31 := Address{Name: "3-1", Code: "code1"}
address33 := Address{Name: "3-3", Code: "code3"}
db.Model(&user3).Association("Addresss").Append([]Address{address31, address33})
}
运行后数据库结果如下
用一个新的关联替换当前关联(原来所有的关联变为新的关联)
func test() {
// 为用户3替换地址3-1 code1 3-2 code2
var user3 User
db.Model(&User{}).Where("id", 3).Find(&user3)
address31 := Address{Name: "3-1", Code: "code1"}
address32 := Address{Name: "3-2", Code: "code2"}
db.Model(&user3).Association("Addresss").Replace([]Address{address31, address32})
}
运行后数据库结果如下
查询用户下地址数量并进行条件筛选
func test() {
var user1 User
db.Model(&User{}).Where("id", 1).Find(&user1)
// 关联计数
i := db.Model(&user1).Association("Addresss").Count()
fmt.Printf("i: %v\n", i)
// 提交件计数
i2 := db.Model(&user1).Where("code IN ?", []string{"code1", "code4"}).Association("Addresss").Count()
fmt.Printf("i2: %v\n", i2)
}
运行结果
如果存在,则删除源模型与参数之间的关系,只会删除引用,不会从数据库中删除这些对象。
func test() {
var user1 User
db.Model(&User{}).Where("id", 1).Find(&user1)
var user12, user13 Address
db.Model(&Address{}).Where("id", 7).Find(&user13)
// 注意这条不属于user1
db.Model(&Address{}).Where("id", 10).Find(&user12)
db.Model(&user1).Association("Addresss").Delete([]Address{user13, user12})
}
运行后数据库
删除源模型与关联之间的所有引用,但不会删除这些关联
func test() {
var user3 User
db.Model(&User{}).Where("id", 3).Find(&user3)
db.Model(&user3).Association("Addresss").Clear()
}
运行后数据库结果如下
查询所有用户时候加载地址信息
func test() {
var users []User
db.Preload("Addresss").Find(&users)
for _, user := range users {
fmt.Printf("user.ID: %v\n", user.ID)
fmt.Printf("user.Name: %v\n", user.Name)
for _, address := range user.Addresss {
fmt.Printf("address.ID: %v\n", address.ID)
fmt.Printf("address.Code: %v\n", address.Code)
}
}
}
运行结果如下
带条件预加载查询用户表id在1,2中的用户并且预加载地址code在code1,code2的地址信息
func test() {
var users []User
db.Where("Id IN ?", []uint{1, 2}).Preload("Addresss", "code IN ?", []string{"code1", "code2"}).Find(&users)
for _, user := range users {
fmt.Printf("user: %v--%v\n", user.ID, user.Name)
for _, address := range user.Addresss {
fmt.Printf("user: %v--%v\n", address.ID, address.Code)
}
}
}
自定义预加载sql
查询用户表id在1,2中的用户并且预加载地信息按照倒序排列
func test() {
var users []User
db.Where("Id IN ?", []uint{1, 2}).
Preload("Addresss", func(db *gorm.DB) *gorm.DB {
return db.Order("ID DESC")
}).
Find(&users)
for _, user := range users {
fmt.Printf("user: %v--%v\n", user.ID, user.Name)
for _, address := range user.Addresss {
fmt.Printf("user: %v--%v\n", address.ID, address.Code)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)