<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go 使用xorm操作mysql

          共 4673字,需瀏覽 10分鐘

           ·

          2021-10-29 08:21

          本文介紹了 golang orm 庫 xorm 的使用和項(xiàng)目結(jié)構(gòu)。

          xorm

          官方介紹:xorm 是一個(gè)簡單而強(qiáng)大的 Go 語言 ORM 庫。通過它可以使數(shù)據(jù)庫操作非常簡便。xorm 的目標(biāo)并不是讓你完全不去學(xué)習(xí) SQL,我們認(rèn)為 SQL 并不會(huì)為 ORM 所替代,但是 ORM 將可以解決絕大部分的簡單 SQL 需求。xorm 支持兩種風(fēng)格的混用。

          xorm 還提供了工具,通過 reverse 命令根據(jù)數(shù)據(jù)庫的表結(jié)構(gòu)生成對應(yīng)的 struct,省去了人工組織代碼的工作,十分方便。官方地址:https://xorm.io/

          安裝

          瀏覽 xorm 的 github 地址,我們要下載 2 個(gè)包,https://github.com/go-xorm

          1、xorm 驅(qū)動(dòng)包,我們使用 xorm 的核心包 2、cmd 工具包,用于使用 reverse 命令生成數(shù)據(jù)表對應(yīng)的 struct

          通過 go get 命令分別下載 2 個(gè)包go get github.com/go-xorm/xormgo get github.com/go-xorm/cmd/xorm下載完成后 github.com 文件夾下會(huì)出現(xiàn) go-xorm 包

          生成數(shù)據(jù)結(jié)構(gòu) struct

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

          我們現(xiàn)在就來生成這 2 張數(shù)據(jù)表的結(jié)構(gòu)模型。

          1、在任意項(xiàng)目下新建一個(gè)文件夾 xorm_models,文件名沒有規(guī)定,為了存放生成的代碼文件。
          2、拷貝 cmd 工具包中的摸板目錄到 xorm_models 下,在文件目錄github.com\go-xorm\cmd\xorm\templates\goxorm下

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

          3、打開 cmd 命令行窗口,進(jìn)入 xorm_models 目錄下,執(zhí)行 reverse 命令:xorm reverse [數(shù)據(jù)庫類型] [數(shù)據(jù)庫連接串] [模板目錄]
          xorm?reverse?mysql?root:112233@tcp(127.0.0.1:3305)/test?charset=utf8?templates/goxorm

          4、數(shù)據(jù)結(jié)構(gòu)代碼會(huì)自動(dòng)生成在 xorm_models/models 目錄下。

          我們能看到生成了和表名同名的 2 個(gè)數(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 支持鏈?zhǔn)降膶懛?code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;outline: 0px;font-size: 14px;font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;border-radius: 4px;color: rgb(233, 105, 0);background: rgb(248, 248, 248);">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?會(huì)根據(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、實(shí)例引擎xorm.NewEngine()5、痛快的調(diào)用

          demo 里提供了我們開發(fā)業(yè)務(wù)上常用的增,刪,改,查單條數(shù)據(jù),查列表,查分頁,事務(wù)等內(nèi)容。如果文章對你有用,請點(diǎn)個(gè)贊。

          轉(zhuǎn)自:segmentfault.com/a/1190000022023758

          文章轉(zhuǎn)載:Go開發(fā)大全

          (版權(quán)歸原作者所有,侵刪)


          點(diǎn)擊下方“閱讀原文”查看更多

          瀏覽 44
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  A黄色一级片 | 青青草国产视频在线观看 | www逼逼| 久久人妻熟女中文字幕av蜜芽 | 亚洲另类在线观看 |