Go 语言 ORM 框架之 xorm

2024-05-06 18:20
文章标签 语言 go 框架 orm xorm

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

1、xorm

1.1、xorm 简介

xorm 是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。

特性

  • 支持 struct 和数据库表之间的灵活映射,并支持自动同步
  • 事务支持
  • 同时支持原始SQL语句和ORM操作的混合执行
  • 使用连写来简化调用
  • 支持使用ID, In, Where, Limit, Join, Having, Table, SQL, Cols等函数和结构体等方式作为条件

1.2、安装环境

go get xorm.io/xorm

 1.3、快速开始

基本的连接信息

    var (username string = "root"password string = "xxxxxx"ip string = "127.0.0.1"port int = 3306dbName = "go_web"charset string = "utf8mb4")dataSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",username ,password,ip ,port ,dbName ,charset )

1.3.1、同步结构体到数据库

1. 创建引擎
    // 1. 创建引擎engine, err := xorm.NewEngine("mysql", dataSource)if err!=nil {println("连接失败")}
2. 创建结构体

        结构体中的每个字段都将对应一个表的字段,我们可以通过标签来告诉 xorm 同步到数据库或使用什么数据类型:

    // 2. 创建结构体type User struct {Id int64Name stringAge intPassword string `xorm:"varchar(200)"`Created time.Time `xorm:"created"`Updated time.Time `xorm:"updated"`}
3. 同步结构体
    // 3. 同步结构体err = engine.Sync(new(User))if err!=nil {println("创建表格失败")}

        同步结构体这个操作可以执行多次,如果我们删除了字段,只需要重新同步即可。但是如果修改了字段,就会把修改后的字段作为一个新的字段,所以一定要注意。

1.3.1、单条数据的插入

        插入数据需要使用 Insert 方法,这个方法需要传入一个对象的指针,因为我们知道,方法中的参数是形参,而且结构体类型是值类型,所以进入 Insert 方法后会拷贝一份,而不是操作我们的实参。

我们可以试着打印插入前后,我们实参的变化:

	// 1. 插入单条数据user1 := User{ Id: 2, Name: "李大喜", Age: 22, Password: "123456",}fmt.Println(user1)res,err := engine.InsertOne(&user1)if err != nil {println("插入失败",err)}else {fmt.Printf("成功插入 %d 条数据\n",res)}fmt.Println(user1)

运行结果:

        可以看到,对象未进入 insert 方法之前,它的两个 time 类型的字段是默认值,但是进入 insert 方法后,被赋值为当前时间。

1.3.2、多条数据的插入

1. 传入多个结构体对象插入
    // 1. 插入多条数据user1 := User{ Id: 3, Name: "刘能", Age: 52, Password: "123456",}user2 := User{ Id: 4, Name: "赵四", Age: 52, Password: "123456",}res,err := engine.Insert(&user1,&user2)if err != nil {println("插入失败",err)}else {fmt.Printf("成功插入 %d 条数据\n",res)}
2. 通过切片插入
	users := make([]User,2)user1 := User{ Id: 3, Name: "刘能", Age: 52, Password: "123456",}user2 := User{ Id: 4, Name: "赵四", Age: 52, Password: "123456",}users[0] = user1users[2] = user2res,err := engine.Insert(&users)if err != nil {println("插入失败",err)}else {fmt.Printf("成功插入 %d 条数据\n",res)}

虽然Go语言中的切片是引用类型,但在我们这里仍然需要传入切片的地址(指针)。

这是因为xorm的Insert方法在处理切片时,如果接收到的是切片的指针,它能够通过该指针获取到切片的实时长度信息。这个长度信息对于数据库的批量插入操作是非常重要的,因为它可以帮助xorm确定一次性能插入多少条记录,从而提高效率。

1.3.3、更新与删除

更新数据
    // 修改用户user := User{Name: "谢永强"}res,_ := engine.ID(1).Update(&user)fmt.Println(res)
删除数据
	// 1. 删除单条数据user := User{Name: "谢永强"}res,_ := engine.ID(1).Delete(&user)fmt.Println(res)
执行 SQL
	// 1. 执行 SQLres,_ := engine.Exec("UPDATE user SET name = '刘海柱' WHERE id = ?",1)fmt.Println(res)

1.3.4、查询与遍历

1. SQL 查询(不常用)
  • Query 最原始的也支持SQL语句查询,返回的结果类型为 []map[string][]byte。
  • QueryString 返回 []map[string]string, QueryInterface 返回 []map[string]interface{}。
	res1,_ := engine.Query("SELECT * FROM user");fmt.Println(res1)res2,_ := engine.QueryString("SELECT * FROM user");fmt.Println(res2)res3,_ := engine.QueryInterface("SELECT * FROM user");fmt.Println(res3)

运行结果:

[map[age:[50 50] created:[50 48 50 52 45 48 52 45 51 48 32 50 48 58 53 53 58 51 53] id:[49] name:[229 136 152 230 181 183 230 159 177] password:[49 50 51 52 53 54] updated:[50 48 50 52 45 48 52 45 51 48 32 50 49 58 49 52 58 50 57]] map[age:[50 50] created:[50 48 50 52 45 48 52 45 51 48 32 50 48 58 53 56 58 52 55] id:[50] name:[230 157 142 229 164 167 229 150 156] password:[49 50 51 52 53 54] updated:[50 48 50 52 45 48 52 45 51 48 32 50 48 58 53 56 58 52 55]] map[age:[53 50] created:[50 48 50 52 45 48 52 45 51 48 32 50 49 58 48 52 58 49 49] id:[51] name:[229 136 152 232 131 189] password:[49 50 51 52 53 54] updated:[50 48 50 52 45 48 52 45 51 48 32 50 49 58 48 52 58 49 49]] map[age:[53 50] created:[50 48 50 52 45 48 52 45 51 48 32 50 49 58 48 52 58 49 49] id:[52] name:[232 181 181 229 155 155] password:[49 50 51 52 53 54] updated:[50 48 50 52 45 48 52 45 51 48 32 50 49 58 48 52 58 49 49]]]
[map[age:22 created:2024-04-30 20:55:35 id:1 name:刘海柱 password:123456 updated:2024-04-30 21:14:29] map[age:22 created:2024-04-30 20:58:47 id:2 name:李大喜 password:123456 updated:2024-04-30 20:58:47] map[age:52 created:2024-04-30 21:04:11 id:3 name:刘能 password:123456 updated:2024-04-30 21:04:11] map[age:52 created:2024-04-30 21:04:11 id:4 name:赵四 password:123456 updated:2024-04-30 21:04:11]]
[map[age:22 created:2024-04-30 20:55:35 id:1 name:刘海柱 password:123456 updated:2024-04-30 21:14:29] map[age:22 created:2024-04-30 20:58:47 id:2 name:李大喜 password:123456 updated:2024-04-30 20:58:47] map[age:52 created:2024-04-30 21:04:11 id:3 name:刘能 password:123456 updated:2024-04-30 21:04:11] map[age:52 created:2024-04-30 21:04:11 id:4 name:赵四 password:123456 updated:2024-04-30 21:04:11]]
2. GET 查询(单行查询)
    // GET 查询user := User{}engine.Get(&user) // 默认查询第一条数据fmt.Println(user) // {1 刘海柱 22 123456 2024-04-30 20:55:35 +0800 CST 2024-04-30 21:14:29 +0800 CST}

指定条件来查询

    user := User{Name: "刘海柱"}// 查询指定用户名 且 根据id降序后的第一条数据engine.Where("name=?",user.Name).Desc("id").Get(&user)fmt.Println(user)

查询指定字段:

    // 获取指定字段值user := User{}var name string// Table(&user) 代表表结构和结构体 User 相同engine.Table(&user).Where("id = 1").Cols("name").Get(&name)fmt.Println(name)
3. Find 查询(多行查询)
    var users []Userengine.Where("id < 5").And("password = 123456").Limit(10).Find(&users)for i := 0; i < len(users);i++ {fmt.Println(users[i])}// 或者使用 range 遍历切片for _,user := range users{fmt.Println(user)}

运行结果: 

{1 刘海柱 22 123456 2024-04-30 20:55:35 +0800 CST 2024-04-30 21:14:29 +0800 CST}
{2 李大喜 22 123456 2024-04-30 20:58:47 +0800 CST 2024-04-30 20:58:47 +0800 CST}
{3 刘能 52 123456 2024-04-30 21:04:11 +0800 CST 2024-04-30 21:04:11 +0800 CST}
{4 赵四 52 123456 2024-04-30 21:04:11 +0800 CST 2024-04-30 21:04:11 +0800 CST}
4. Count 查询
    // 查询所有 password="123456" 的用户user := User{Password: "123456"}count,_ := engine.Count(&user)fmt.Println(count) // 4
5. Iterate 迭代查询

Iterate 方法有两个参数:

  1. 查询条件(哪个结构体对应的表以及过滤条件)
  2. 匿名函数(第一个参数代表查询到的当前元素的索引,第二个参数代表当前元素对象)
    engine.Iterate(&User{Password: "123456"}, func(idx int, bean interface{}) error {// 类型断言user := bean.(*User)fmt.Println(user)return nil})
6. Rows 迭代查询
    rows,_ := engine.Rows(&User{Password: "123456"})defer rows.Close()user := new(User) // 传递指针给userfor rows.Next(){rows.Scan(user)fmt.Println(user)}

        这里的 Scan 方法需要一个指针,所以我们上面使用 new(User) 来初始化一个指针给 user,我们也可以通过下面的方法,都是一样的:

	rows,_ := engine.Rows(&User{Password: "123456"})defer rows.Close()user := User{}for rows.Next(){rows.Scan(&user)fmt.Println(user)}

1.3.5、事务

        要使用事务的话,必须使用 session 类进行数据的增删改,并结合 panic 和 recover 来进行异常的处理,一旦出现异常就回滚所有事务:

	session := engine.NewSession()defer session.Close()// 通过 panic 和 recover 进行异常的处理defer func() {err := recover()if err != nil {fmt.Println("Rollback")session.Rollback()}else {session.Commit()}}()session.Begin() // 开启事务user := User{Id: 5,Name: "王老七",Age: 40}if _,err := session.Insert(&user);err!=nil{panic(err)}

这篇关于Go 语言 ORM 框架之 xorm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

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

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX

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

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