Go语言数据库管理:深入探索Gorm V2

发表时间: 2024-05-14 22:07

github地址:

https://github.com/go-gorm/gorm

官方文档:

https://gorm.io/docs/

中文文档

https://gorm.io/zh_CN/docs/index.html

https://www.bookstack.cn/read/gorm-2.0/index.md

go get -u gorm.io/gormgo get -u gorm.io/driver/mysql

系统自带四个字段

gorm.Model

type Model struct {ID uint `gorm:"primary_key"`CreatedAt time.TimeUpdatedAt time.TimeDeletedAt *time.Time `sql:"index"`}

迁移表的字段名称:

id

CreatedAt

updated_at

deleted_at

type User struct {    ID uint // column name is `id`    Name string // column name is `name`    Birthday time.Time // column name is `birthday`    CreatedAt time.Time // column name is `created_at`}

当然你也可以自定义

type Animal struct {    AnimalID int64 `gorm:"column:beast_id"` // set name to `beast_id`    Birthday time.Time `gorm:"column:day_of_the_beast"` // set name to `day_of_the_beast`    Age int64 `gorm:"column:age_of_the_beast"` // set name to `age_of_the_beast`}

实例:

package mainimport (    "time"    "gorm.io/driver/mysql"    "gorm.io/gorm")type Product struct {    gorm.Model    Code string    Price uint}func main() {    dsn := "root:guo456@tcp(127.0.0.1:3306)/blog?charset=utf8mb4&parseTime=True&loc=Local"    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})    // 连接池 V2相对v1多出的连接池功能    // 获取通用数据库对象 sql.DB ,然后使用其提供的功能    sqlDB, err := db.DB()    // SetMaxIdleConns 用于设置连接池中空闲连接的最大数量。    sqlDB.SetMaxIdleConns(10)    // SetMaxOpenConns 设置打开数据库连接的最大数量。    sqlDB.SetMaxOpenConns(100)    // SetConnMaxLifetime 设置了连接可复用的最大时间。    sqlDB.SetConnMaxLifetime(time.Hour)    if err != nil {    panic("failed to connect database")    }    // defer db.Close()    // Migrate the schema    db.AutoMigrate(&Product{})    // Create    db.Create(&Product{Code: "L1212", Price: 1000})    // Read    var product Product    db.First(&product, 1) // find product with id 1    db.First(&product, "code = ?", "L1212") // find product with code l1212    // Update - update product's price to 2000    db.Model(&product).Update("Price", 2000)    // Delete - delete product    db.Delete(&product)}
mysql> desc products;+------------+---------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+------------+---------------------+------+-----+---------+----------------+| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment || created_at | datetime(3) | YES | | NULL | || updated_at | datetime(3) | YES | | NULL | || deleted_at | datetime(3) | YES | MUL | NULL | || code | longtext | YES | | NULL | || price | bigint(20) unsigned | YES | | NULL | |+------------+---------------------+------+-----+---------+----------------+6 rows in set (0.00 sec)mysql> select * from products;+----+-------------------------+-------------------------+-------------------------+-------+-------+| id | created_at | updated_at | deleted_at | code | price |+----+-------------------------+-------------------------+-------------------------+-------+-------+| 1 | 2021-08-14 11:19:41.000 | 2021-08-14 11:19:41.016 | 2021-08-14 11:19:41.016 | L1212 | 2000 |+----+-------------------------+-------------------------+-------------------------+-------+-------+1 row in set (0.00 sec)

zapgorm2 - Uber 的 Zap 日志驱动程序, 适用版本gorm V2

https://github.com/moul/zapgorm2