06.GORMv2
# 01.gorm基础
# 1.1 安装
go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite
1
2
2
# 1.2 增删改查
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func main() {
dsn := "root:1@tcp(127.0.0.1:3306)/m_test?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// 1、迁移 schema
db.AutoMigrate(&Product{})
// 2、增
db.Create(&Product{Code: "D42", Price: 100})
// 3、读
var product Product
db.First(&product, 1) // 根据整形主键查找
db.First(&product, "code = ?", "D42") // 查找 code 字段值为 D42 的记录
// 4、改
db.Model(&product).Update("Price", 200)
//// Update - 更新多个字段
//db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // 仅更新非零值字段
//db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})
// 5、删
db.Delete(&product, 1)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 02.模型定义
# 2.1 定义模型
type User struct {
ID uint
Name string
Email *string
Age uint8
Birthday *time.Time
MemberNumber sql.NullString
ActivedAt sql.NullTime
CreatedAt time.Time
UpdatedAt time.Time
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2.2 gorm.Model
- GORM 定义一个
gorm.Model
结构体,其包括字段ID
、CreatedAt
、UpdatedAt
、DeletedAt
// gorm.Model 的定义
type Model struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 03.gorm基本使用
├── main.go
├── models
│ ├── core.go
│ ├── curd.go
│ └── user.go
1
2
3
4
5
2
3
4
5
# 3.1 mian.go
package main
import (
"gorm_learn/models"
)
func main() {
models.InitConn()
// 迁移 schema
// models.DB.AutoMigrate(&models.User{})
models.AddUser()
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 3.2 models/core.go
package models
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
var err error
func InitConn() {
dsn := "root:1@tcp(127.0.0.1:3306)/m_test?charset=utf8mb4&parseTime=True&loc=Local"
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("gorm conn err")
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 3.3 model/user.go
package models
type User struct {
Id int
Username string
Age int
Email string
AddTime int
}
//定义结构体操作的数据库表
func (User) TableName() string {
return "user"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3.4 model/curd.go
package models
import "time"
// 1、增
func AddUser() {
user := User{
Username: "zhangsan",
Age: 26,
Email: "zhangsan.qq.com",
AddTime: int(time.Now().Unix()),
}
DB.Create(&user)
}
// 2、查
func SelectUser() {
user := []User{}
DB.Find(&user)
}
// 3、改
func UserEdit() {
//1、查找username=zhangsan
user := User{Username: "zhangsan"}
DB.First(&user)
//2、执行修改为 lisi
user.Username = "lisi"
DB.Save(&user)
}
// 4、删
func UserDel() {
DB.Where("username = ?", "zhangsan").Delete(&User{})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 04.锁
package main
import (
"fmt"
"gorm.io/gorm"
"gorm_learn/models"
)
func main() {
models.InitConn()
column := models.DB.Model(&models.User{}).Where("id", 43).UpdateColumn("username", "wangwu")
fmt.Println(column.RowsAffected)
if column.RowsAffected == 0 {
fmt.Println("未作修改")
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
上次更新: 2024/3/13 15:35:10