Go?使用xorm操作mysql詳情
xorm
官方介紹:xorm 是一個簡單而強大的 Go 語言 ORM 庫。
通過它可以使數(shù)據(jù)庫操作非常簡便。
xorm 的目標并不是讓你完全不去學(xué)習(xí) SQL,我們認為 SQL 并不會為 ORM 所替代,但是 ORM 將可以解決絕大部分的簡單 SQL 需求。
xorm 支持兩種風(fēng)格的混用。
xorm 還提供了工具,通過 reverse 命令根據(jù)數(shù)據(jù)庫的表結(jié)構(gòu)生成對應(yīng)的 struct,省去了人工組織代碼的工作,十分方便。官方地址:https://xorm.io/
安裝
瀏覽 xorm 的 github 地址,我們要下載 2 個包,https://github.com/go-xorm

1、xorm 驅(qū)動包,我們使用 xorm 的核心包 2、cmd 工具包,用于使用 reverse 命令生成數(shù)據(jù)表對應(yīng)的 struct
通過 go get 命令分別下載 2 個包go get github.com/go-xorm/xorm``go get github.com/go-xorm/cmd/xorm
下載完成后 github.com 文件夾下會出現(xiàn) go-xorm 包

生成數(shù)據(jù)結(jié)構(gòu) struct
本地數(shù)據(jù)庫 test 有 2 張數(shù)據(jù)表,doctor_tb 和 user_tb, 數(shù)據(jù)結(jié)構(gòu)如下:

我們現(xiàn)在就來生成這 2 張數(shù)據(jù)表的結(jié)構(gòu)模型。
1、在任意項目下新建一個文件夾 xorm_models,文件名沒有規(guī)定,為了存放生成的代碼文件。
2、拷貝 cmd 工具包中的摸板目錄到 xorm_models 下,在文件目錄github.com\go-xorm\cmd\xorm\templates\goxorm下

config 是生成的配置信息,struct.go.tpl 是數(shù)據(jù)摸板,允許自定義,可以根據(jù)自己的項目需求,修改摸板。一般不需要修改。

我們能看到生成了和表名同名的 2 個數(shù)據(jù)結(jié)構(gòu)文件 doctor_tb.go 和 user_tb.go
package models ?
import ( ?
? ? "time" ?
) ?
type DoctorTb struct { ?
? ? Id ? ? ?int ? ? ? `xorm:"not null pk autoincr INT(11)"` ?
? ? Name ? ?string ? ?`xorm:"default '' comment('姓名') VARCHAR(50)"` ?
? ? Age ? ? int ? ? ? `xorm:"default 0 comment('年齡') INT(11)"` ?
? ? Sex ? ? int ? ? ? `xorm:"default 0 comment('性別') INT(11)"` ?
? ? Addtime time.Time `xorm:"DATETIME"` ?
} ?使用 xorm
xorm 支持鏈式的寫法
engine.Where("age > ?", 40).Or("name like ?", "林%").OrderBy("Id desc").Find(&docList2) 也支持直接執(zhí)行 sql 語句engine.SQL("select * from doctor_tb where age > ?", 40).Find(&docList4)
附上增刪改查事務(wù)的 demo 例子,代碼里都有注釋,很容易看懂。xorm 的封裝比較友好,只要熟悉 sql 語句,即便不看文檔,也能順利的使用各種關(guān)鍵字。
package main ?
import ( ?
? ? "fmt" ?
? ? _ "github.com/go-sql-driver/mysql" ?
? ? "github.com/go-xorm/xorm" ?
? ? "goShare/xorm_models/models" ?
? ? "time" ?
) ?
func main() { ?
? ? var engine *xorm.Engine ?
? ? //連接數(shù)據(jù)庫 ?
? ? engine, err := xorm.NewEngine("mysql", "root:112233@tcp(127.0.0.1:3305)/test?charset=utf8") ?
? ? if err != nil { ?
? ? ? ? fmt.Println(err) ?
? ? ? ? return ?
? ? } ?
? ? //連接測試 ?
? ? if err := engine.Ping(); err != nil { ?
? ? ? ? fmt.Println(err) ?
? ? ? ? return ?
? ? } ?
? ? defer engine.Close() //延遲關(guān)閉數(shù)據(jù)庫 ?
? ? fmt.Println("數(shù)據(jù)庫鏈接成功") ?
? ? //查詢單條數(shù)據(jù) ?
? ? var doc models.DoctorTb ?
? ? b, _ := engine.Where("name = ?", "鐘南山").Get(&doc) ?
? ? if b { ?
? ? ? ? fmt.Println(doc) ?
? ? } else { ?
? ? ? ? fmt.Println("數(shù)據(jù)不存在") ?
? ? } ?
? ? //查詢單條數(shù)據(jù)方式 2 會根據(jù)結(jié)構(gòu)體的 ?
? ? doc2 := models.DoctorTb{Name: "鐘南山"} ?
? ? b, _ = engine.Get(&doc2) ?
? ? fmt.Println(doc2) ?
? ? //新增數(shù)據(jù) ?
? ? doc3 := models.DoctorTb{0, "王醫(yī)生", 48, 1, time.Now()} ?
? ? i3, _ := engine.InsertOne(doc3) ?
? ? fmt.Println("新增結(jié)果:", i3) ?
? ? //查詢列表 ?
? ? docList := make([]models.DoctorTb, 0) ?
? ? engine.Where("age > ? or name like ?", 40, "林%").Find(&docList) ?
? ? fmt.Println("docList:", docList) ?
? ? //查詢列表方式 2 ?
? ? docList2 := make([]models.DoctorTb, 0) ?
? ? engine.Where("age > ?", 40).Or("name like ?", "林%").OrderBy("Id desc").Find(&docList2) ?
? ? fmt.Println("docList2:", docList2) ?
? ? //查詢分頁 ?
? ? docList3 := make([]models.DoctorTb, 0) ?
? ? page := 0 ? ? //頁索引 ?
? ? pageSize := 2 //每頁數(shù)據(jù) ?
? ? limit := pageSize ?
? ? start := page * pageSize ?
? ? totalCount, err := engine.Where("age > ? or name like ?", 40, "林%").Limit(limit, start).FindAndCount(&docList3) ?
? ? fmt.Println("總記錄數(shù):", totalCount, "docList3:", docList3) ?
? ? //直接用語句查詢 ?
? ? docList4 := make([]models.DoctorTb, 0) ?
? ? engine.SQL("select * from doctor_tb where age > ?", 40).Find(&docList4) ?
? ? fmt.Println("docList4:", docList4) ?
? ? //刪除 ?
? ? docDel := models.DoctorTb{Name: "王醫(yī)生"} ?
? ? iDel, _ := engine.Delete(&docDel) ?
? ? fmt.Println("刪除結(jié)果:", iDel) ?
? ? //刪除方式 2 ?
? ? engine.Exec("delete from doctor_tb where Id = ?", 3) ?
? ? //更新數(shù)據(jù) ?
? ? doc5 := models.DoctorTb{Name: "鐘醫(yī)生"} ?
? ? //更新數(shù)據(jù) ID 為 2 的記錄名字更改為“鐘醫(yī)生” ?
? ? iUpdate, _ := engine.Id(2).Update(&doc5) ?
? ? fmt.Println("更新結(jié)果:", iUpdate) ?
? ? //指定表名查詢。Table() ?
? ? user := models.UserTb{Id: 2} ?
? ? b, _ = engine.Table("user_tb").Get(&user) ?
? ? fmt.Println(user) ?
? ? //事務(wù) ?
? ? session := engine.NewSession() ?
? ? defer session.Close() ?
? ? err = session.Begin() ?
? ? _, err = session.Exec("delete from doctor_tb where Id = ?", 6) ?
? ? if err != nil { ?
? ? ? ? session.Rollback() ?
? ? ? ? return ?
? ? } ?
? ? _, err = session.Exec("delete from user_tb where Id = ?", 10) ?
? ? if err != nil { ?
? ? ? ? session.Rollback() ?
? ? ? ? return ?
? ? } ?
? ? err = session.Commit() ?
? ? if err != nil { ?
? ? ? ? return ?
? ? } ?
? ? fmt.Println("事務(wù)執(zhí)行成功") ?
} ?總結(jié):
歸納下使用流程 1、下載 xorm 包和 cmd 工具包 2、復(fù)制 cmd 工具包里的模板代碼文件夾至生成目錄底下 3、使用 reverse 生成數(shù)據(jù)結(jié)構(gòu)代碼,省去苦力活 4、實例引擎
xorm.NewEngine()
5、痛快的調(diào)用
demo 里提供了我們開發(fā)業(yè)務(wù)上常用的增,刪,改,查單條數(shù)據(jù),查列表,查分頁,事務(wù)等內(nèi)容。
到此這篇關(guān)于Go 使用xorm操作mysql詳情的文章就介紹到這了,更多相關(guān)Go 使用xorm操作mysql內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go中調(diào)用JS代碼(otto)的實現(xiàn)示例
Otto是一個用Go語言實現(xiàn)的JavaScript解釋器,可用于執(zhí)行和操作JavaScript代碼,適合在Go項目中執(zhí)行簡單的JS腳本,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
Golang中的Slice與數(shù)組及區(qū)別詳解
數(shù)組是一種具有固定長度的基本數(shù)據(jù)結(jié)構(gòu),在golang中與C語言一樣數(shù)組一旦創(chuàng)建了它的長度就不允許改變,數(shù)組的空余位置用0填補,不允許數(shù)組越界。今天小編通過實例代碼操作給大家詳細介紹lang中的Slice與數(shù)組的相關(guān)知識,一起看看吧2020-02-02
Golang中unicode碼和中文的互相轉(zhuǎn)換函數(shù)使用
這篇文章主要為大家介紹了Golang中unicode碼和中文的互相轉(zhuǎn)換函數(shù)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Go?通過?Map/Filter/ForEach?等流式?API?高效處理數(shù)據(jù)的思路詳解
Stream?的實現(xiàn)思想就是將數(shù)據(jù)處理流程抽象成了一個數(shù)據(jù)流,每次加工后返回一個新的流供使用。這篇文章主要介紹了Go?通過?Map/Filter/ForEach?等流式?API?高效處理數(shù)據(jù),需要的朋友可以參考下2022-01-01
Go 通過結(jié)構(gòu)struct實現(xiàn)接口interface的問題
這篇文章主要介紹了Go 通過結(jié)構(gòu)struct實現(xiàn)接口interface的問題,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10

