grom-V2-05-初始化

2022-02-14  本文已影响0人  玄德公笔记

1. FirstOrInit

注意
FirstOrInit只会赋值给结构体,而不会写入表
FirstOrCreate会写入表中

1.1 基本使用

1.1.1 使用说明

根据参数查表,
查到,则赋值给结构体;
没查到,结构体只能得到查询条件的值。

db.FirstOrInit(&user, User{Name: "LiuBei"})

1.1.2 完整示例

package main

import (
    "database/sql"
    "fmt"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "time"
)

type User struct {
    ID int64
    Age int64
    Name string
    Email string
    Company string
}



func main() {
    db,sqlDB,_ := connect()
    defer sqlDB.Close()

    var user User
    db.FirstOrInit(&user, User{Name: "LiuBei"})
    fmt.Println(user)
}

func connect() (db *gorm.DB,sqlDB *sql.DB,err error) {
    dsn := "root:40010355@tcp(127.0.0.1:3306)/crow?charset=utf8&parseTime=True&loc=Local"
    db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
    sqlDB,_ = db.DB()
    if err != nil {
        fmt.Printf(err.Error())
        defer sqlDB.Close()
    }else {
        fmt.Printf("OK\n")
        sqlDB.SetMaxIdleConns(10)
        sqlDB.SetMaxOpenConns(100)
        sqlDB.SetConnMaxLifetime(time.Hour)
    }
    return
}

查到,则将结果赋值给结构体:

OK
{1 28 LiuBei liubei@xishu.com shu}

未查到,则结构他仅获得查询条件的值:

{0 0 LiuBei  }

1.2 Attrs (未查到结果时给Struct赋值)

1.2.1 使用说明

Attrs写在FirstOrInit前,FirstOrInit没有查到结果时,将接收Attrs的参数的数据

db.Attrs(GuanYu).FirstOrInit(&user, User{Name: "GuanYu"})

1.2.2 示例

mysql> select * from users;
+----+----------+------+--------------------+---------+
| id | name     | age  | email              | company |
+----+----------+------+--------------------+---------+
|  1 | LiuBei   |   28 | liubei@xishu.com   | shu     |
|  2 | GuanYu   |   22 | guanyu@xishu.com   | shu     |
|  3 | ZhangFei |   18 | zhangfei@xishu.com | shu     |
+----+----------+------+--------------------+---------+
3 rows in set (0.00 sec)
func main() {
    db,sqlDB,_ := connect()
    defer sqlDB.Close()
    GuanYu := User{
        ID: 3,
        Name: "GuanYu",
        Age: 22,
        Email: "guanyu@xishu.com",
        Company: "shu",
    }

    var user User
    db.Attrs(GuanYu).FirstOrInit(&user, User{Name: "GuanYu"})
    fmt.Println(user)
}

因为查到关羽,则输出了关羽的信息

OK
{2 22 GuanYu guanyu@xishu.com shu}
OK
{0 99 WuSheng guanyu@xishu.com shen}

1.3 Assign(不论是否查到都将参数赋值给 struct)

1.3.1 使用说明

db.Where(xiShu{Name: "LiuBei"}).Assign(xiShu{Age: 35}).FirstOrInit(&user)

1.3.2 示例

func main() {
    db,sqlDB,_ := connect()
    defer sqlDB.Close()
    GuanYu := User{
        Name: "WuSheng",
        Company: "shen",
    }

    var user User
    db.Assign(GuanYu).FirstOrInit(&user, User{Name: "GuanYu"})
    fmt.Println(user)
}
OK
{2 22 WuSheng guanyu@xishu.com shen}
OK
{0 0 WuSheng  shen}

2. FirstOrCreate

2.1 基本使用

2.1.1 使用说明

2.2.2 示例

func main() {
    db,sqlDB,_ := connect()
    defer sqlDB.Close()

    var user User
    db.FirstOrCreate(&user, User{Name: "ZhaoYun"})
    fmt.Println(user)
}
OK
{31 0 ZhaoYun  }

这条数据仅接收到查询条件

mysql> select * from users;
+----+----------+------+--------------------+---------+
| id | name     | age  | email              | company |
+----+----------+------+--------------------+---------+
|  1 | LiuBei   |   28 | liubei@xishu.com   | shu     |
|  2 | GuanYu   |   22 | guanyu@xishu.com   | shu     |
|  3 | ZhangFei |   18 | zhangfei@xishu.com | shu     |
| 31 | ZhaoYun  |    0 |                    |         |
+----+----------+------+--------------------+---------+
4 rows in set (0.00 sec)

查到结果会赋值给接收的结构体,表中不会增加数据。

OK
{31 18 ZhaoYun ZhaoYun@xishu.com shu}

2.2 Attrs

db.Attrs(ZhaoYun).FirstOrCreate(&user, User{Name: "ZhaoYun"})

2.3 Assign

db.Assign(ZhaoYun).FirstOrCreate(&user, User{Name: "ZhaoYun"})
上一篇 下一篇

猜你喜欢

热点阅读