go的orm框架-Gorm

2024-04-03 05:12
文章标签 go 框架 gorm orm

本文主要是介绍go的orm框架-Gorm,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官网文档

特点

  • 全功能 ORM
  •  关联 (拥有一个,拥有多个,属于,多对多,多态,单表继承)
  •  Create,Save,Update,Delete,Find 中钩子方法
  •  支持 Preload、Joins 的预加载
  •  事务,嵌套事务,Save Point,Rollback To to Saved Point
  •  Context、预编译模式、DryRun 模式
  •  批量插入,FindInBatches,Find/Create with Map,使用 SQL 表达式、Context Valuer 进行 CRUD
  •  SQL 构建器,Upsert,锁,Optimizer/Index/Comment Hint,命名参数,子查询
  •  复合主键,索引,约束
  •  自动迁移
  •  自定义 Logger
  •  灵活的可扩展插件 API:Database Resolver(多数据库,读写分离)、Prometheus…
  •  每个特性都经过了测试的重重考验
  •  开发者友好

约定(重点)

主键

GORM 使用一个名为ID每个模型的默认主键的字段。

type User struct {ID   string // 默认情况下,名为 `ID` 的字段会作为表的主键Name string
}

可以通过标签 primaryKey 将其它字段设为主键 

// 将 `UUID` 设为主键
type Animal struct {ID     int64UUID   string `gorm:"primaryKey"`Name   stringAge    int64
}

表名称

默认情况下,GORM 将结构名称转换为snake_case表名称并将其复数化。例如,一个User结构体出现users在数据库中。

您可以实现 Tabler 接口来更改默认表名,例如:

type Tabler interface {TableName() string
}// TableName 会将 User 的表名重写为 `profiles`
func (User) TableName() string {return "profiles"
}

注意: TableName 不支持动态变化,它会被缓存下来以便后续使用。想要使用动态表名,你可以使用 Scopes,例如: 

func UserTable(user User) func (tx *gorm.DB) *gorm.DB {return func (tx *gorm.DB) *gorm.DB {if user.Admin {return tx.Table("admin_users")}return tx.Table("users")}
}db.Scopes(UserTable(user)).Create(&user)

临时指定表名

您可以使用 Table 方法临时指定表名,例如:

// 根据 User 的字段创建 `deleted_users` 表
db.Table("deleted_users").AutoMigrate(&User{})// 从另一张表查询数据
var deletedUsers []User
db.Table("deleted_users").Find(&deletedUsers)
// SELECT * FROM deleted_users;db.Table("deleted_users").Where("name = ?", "jinzhu").Delete(&User{})
// DELETE FROM deleted_users WHERE name = 'jinzhu';

列名

GORM 自动将结构体字段名转换为snake_case数据库中的列名。

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

您可以使用 column 标签或 命名策略 来覆盖列名 

type Animal struct {AnimalID int64     `gorm:"column:beast_id"`         // 将列名设为 `beast_id`Birthday time.Time `gorm:"column:day_of_the_beast"` // 将列名设为 `day_of_the_beast`Age      int64     `gorm:"column:age_of_the_beast"` // 将列名设为 `age_of_the_beast`
}

 你可以通过将 autoCreateTime 标签置为 false 来禁用时间戳追踪,例如:

type User struct {CreatedAt time.Time `gorm:"autoCreateTime:false"`
}

 

时间戳字段

GORM 使用名为CreatedAt和的字段UpdatedAt来自动跟踪记录的创建和更新时间。

对于有 CreatedAt 字段的模型,创建记录时,如果该字段值为零值,则将该字段的值设为当前时间

db.Create(&user) // 将 `CreatedAt` 设为当前时间user2 := User{Name: "jinzhu", CreatedAt: time.Now()}
db.Create(&user2) // user2 的 `CreatedAt` 不会被修改// 想要修改该值,您可以使用 `Update`
db.Model(&user).Update("CreatedAt", time.Now())

 对于有 UpdatedAt 字段的模型,更新记录时,将该字段的值设为当前时间。创建记录时,如果该字段值为零值,则将该字段的值设为当前时间

db.Save(&user) // 将 `UpdatedAt` 设为当前时间db.Model(&user).Update("name", "jinzhu") // 会将 `UpdatedAt` 设为当前时间db.Model(&user).UpdateColumn("name", "jinzhu") // `UpdatedAt` 不会被修改user2 := User{Name: "jinzhu", UpdatedAt: time.Now()}
db.Create(&user2) // 创建记录时,user2 的 `UpdatedAt` 不会被修改user3 := User{Name: "jinzhu", UpdatedAt: time.Now()}
db.Save(&user3) // 更新时,user3 的 `UpdatedAt` 会修改为当前时间

 你可以通过将 autoUpdateTime 标签置为 false 来禁用时间戳追踪,例如:

type User struct {UpdatedAt time.Time `gorm:"autoUpdateTime:false"`
}

安装

要在有mod文件的文件夹下面执行下面的命令

go get -u gorm.io/gorm

连接到数据库(mysql)

安装mysql驱动

go get -u gorm.io/driver/mysql

 

编写测试连接 

基础版

package main
import ("fmt""gorm.io/driver/mysql""gorm.io/gorm"
)
func main() {// 参考 root:123456@tcp(192.168.31.131:3306)/gotestdsn := "root:123456@tcp(192.168.31.131:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})if err != nil {fmt.Println("连接失败")return}fmt.Println("连接成功", db)
}

高级版

 

package main
import ("fmt""gorm.io/driver/mysql""gorm.io/gorm"
)
func main() {// 参考 root:123456@tcp(192.168.31.131:3306)/gotestdsn := "root:123456@tcp(192.168.31.131:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"db, err := gorm.Open(mysql.New(mysql.Config{DSN:                       dsn,   // DSN data source nameDefaultStringSize:         256,   // string 类型字段的默认长度DisableDatetimePrecision:  true,  // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持DontSupportRenameIndex:    true,  // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引DontSupportRenameColumn:   true,  // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置}), &gorm.Config{})if err != nil {fmt.Println("连接失败")return}fmt.Println("连接成功", db)
}

 crud

准备数据库和结构体

package main
import ("errors""fmt""gorm.io/driver/mysql""gorm.io/gorm"
)
type Stu struct {Id      int `gorm:"primaryKey"`Name    stringAge     intAddress string
}func getDb() *gorm.DB {dsn := "root:123456@tcp(192.168.31.131:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})if err != nil {fmt.Println("连接失败")panic(errors.New("连接失败"))}fmt.Println("连接成功", db)return db
}

注意结构体中的属性首字母要大写,否则就不可见,还要指定主键 

查询

需要带条件的可以自己查看api

查询一个

first
 获取第一条记录(主键升序)
func main() {db := getDb()testFirst(db)
}
func testFirst(db *gorm.DB) {var stu Stures := db.Table("stu").First(&stu)if res.Error != nil {fmt.Println("查询数据失败")return}fmt.Println(stu)
}

Take
获取一条记录,没有指定排序字段
func testTake(db *gorm.DB) {var stu Stures := db.Table("stu").Take(&stu)if res.Error != nil {fmt.Println("查询数据失败")return}fmt.Println(stu)
}
Last
获取最后一条记录(主键降序)
func testLast(db *gorm.DB) {var stu Stures := db.Table("stu").Last(&stu)if res.Error != nil {fmt.Println("查询数据失败")return}fmt.Println(stu)
}

批量查询

func testMany(db *gorm.DB) {var stus = make([]Stu, 0)//相当于条件是id为10_ = db.Table("stu").Find(&stus)fmt.Println(stus)
}

新增

如果表不存在,会创建表

新增一个

func testAddOne(db *gorm.DB) {stu := Stu{Name: "新增名称", Age: 11, Address: "新增地址"}tx := db.Table("stu").Create(&stu)if tx.Error != nil {fmt.Println("新增失败")return}fmt.Println(stu.Id)
}

批量新增

func testAddMany(db *gorm.DB) {stus := []Stu{{Name: "批量新增名称1", Age: 11, Address: "批量新增地址1"}, {Name: "批量新增名称2", Age: 11, Address: "批量新增地址2"}}tx := db.Table("stu").Create(&stus)if tx.Error != nil {fmt.Println("新增失败")return}fmt.Println(stus)
}

更新

更新一个

func testUpdateOne(db *gorm.DB) {var stu Stu_ = db.Table("stu").First(&stu)stu.Name = "更新后名字"db.Table("stu").Save(&stu)
}
func testUpdateOne1(db *gorm.DB) {db.Table("stu").Where("id=?", 72).Update("name", "更新").Update("address", "跟新地址")
}

批量更新

func testUpdateMany(db *gorm.DB) {db.Table("stu").Where("id in (?)", []int{1,2,3,43}).Update("name", "更新").Update("address", "跟新地址")
}

删除

func testDeleteMany(db *gorm.DB) {db.Table("stu").Where("id in (?)", []int{1, 2, 3, 43}).Delete(&Stu{})
}

 

这篇关于go的orm框架-Gorm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/871981

相关文章

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

数据治理框架-ISO数据治理标准

引言 "数据治理"并不是一个新的概念,国内外有很多组织专注于数据治理理论和实践的研究。目前国际上,主要的数据治理框架有ISO数据治理标准、GDI数据治理框架、DAMA数据治理管理框架等。 ISO数据治理标准 改标准阐述了数据治理的标准、基本原则和数据治理模型,是一套完整的数据治理方法论。 ISO/IEC 38505标准的数据治理方法论的核心内容如下: 数据治理的目标:促进组织高效、合理地

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

【Kubernetes】K8s 的安全框架和用户认证

K8s 的安全框架和用户认证 1.Kubernetes 的安全框架1.1 认证:Authentication1.2 鉴权:Authorization1.3 准入控制:Admission Control 2.Kubernetes 的用户认证2.1 Kubernetes 的用户认证方式2.2 配置 Kubernetes 集群使用密码认证 Kubernetes 作为一个分布式的虚拟

Spring Framework系统框架

序号表示的是学习顺序 IoC(控制反转)/DI(依赖注入): ioc:思想上是控制反转,spring提供了一个容器,称为IOC容器,用它来充当IOC思想中的外部。 我的理解就是spring把这些对象集中管理,放在容器中,这个容器就叫Ioc这些对象统称为Bean 用对象的时候不用new,直接外部提供(bean) 当外部的对象有关系的时候,IOC给它俩绑好(DI) DI和IO

【Go】go连接clickhouse使用TCP协议

离开你是傻是对是错 是看破是软弱 这结果是爱是恨或者是什么 如果是种解脱 怎么会还有眷恋在我心窝 那么爱你为什么                      🎵 黄品源/莫文蔚《那么爱你为什么》 package mainimport ("context""fmt""log""time""github.com/ClickHouse/clickhouse-go/v2")func main(