av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)GoFrame教程:GoFrame數(shù)據(jù)庫ORM-方法操作

方法操作

方法操作用于原生?SQL?執(zhí)行,相對鏈?zhǔn)讲僮鞲讓硬僮饕恍?ORM?鏈?zhǔn)讲僮鲌?zhí)行不了太過于復(fù)雜的?SQL?操作時,可以交給方法操作來處理。

成都創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,先為廣漢等服務(wù)建站,廣漢等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為廣漢企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

接口文檔: https://pkg.GO.dev/github.com/gogf/gf/v2/database/gdb

常用方法:

本文檔的方法列表可能滯后于于代碼,詳細(xì)的方法列表請查看接口文檔,以下方法僅供參考。

// SQL操作方法,返回原生的標(biāo)準(zhǔn)庫sql對象
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Prepare(ctx context.Context, query string) (*sql.Stmt, error)

// 數(shù)據(jù)表記錄查詢:
// 查詢單條記錄、查詢多條記錄、獲取記錄對象、查詢單個字段值(鏈?zhǔn)讲僮魍? 
GetAll(ctx context.Context, sql string, args ...interface{}) (Result, error)
GetOne(ctx context.Context, sql string, args ...interface{}) (Record, error)
GetValue(ctx context.Context, sql string, args ...interface{}) (Value, error)
GetArray(ctx context.Context, sql string, args ...interface{}) ([]Value, error)
GetCount(ctx context.Context, sql string, args ...interface{}) (int, error)
GetScan(ctx context.Context, objPointer interface{}, sql string, args ...interface{}) error

// 數(shù)據(jù)單條操作
Insert(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)
Replace(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)
Save(ctx context.Context, table string, data interface{}, batch...int) (sql.Result, error)

// 數(shù)據(jù)修改/刪除
Update(ctx context.Context, table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
Delete(ctx context.Context, table string, condition interface{}, args ...interface{}) (sql.Result, error)

簡要說明:

  1. ?Query?是原始的數(shù)據(jù)查詢方法,返回的是原生的標(biāo)準(zhǔn)庫的結(jié)果集對象,需要自行解析。推薦使用?Get*?方法,會對結(jié)果自動做解析。
  2. ?Exec?方法用于寫入/更新的?SQL?的操作。
  3. 在執(zhí)行數(shù)據(jù)查詢時推薦使用?Get*?系列查詢方法。
  4. ?Insert/Replace/Save?方法中的?data?參數(shù)支持的數(shù)據(jù)類型為:?string/map/slice/struct/*struct?,當(dāng)傳遞為?slice?類型時,自動識別為批量操作,此時?batch?參數(shù)有效。

操作示例

1. ORM對象

// 獲取默認(rèn)配置的數(shù)據(jù)庫對象(配置名稱為"default")
db := g.DB()

// 獲取配置分組名稱為"user-center"的數(shù)據(jù)庫對象
db := g.DB("user-center")

// 使用原生單例管理方法獲取數(shù)據(jù)庫對象單例
db, err := gdb.Instance()
db, err := gdb.Instance("user-center")

// 注意不用的時候不需要使用Close方法關(guān)閉數(shù)據(jù)庫連接(并且gdb也沒有提供Close方法),
// 數(shù)據(jù)庫引擎底層采用了鏈接池設(shè)計,當(dāng)鏈接不再使用時會自動關(guān)閉

2. 數(shù)據(jù)寫入

r, err := db.Insert(ctx, "user", gdb.Map {
    "name": "john",
})

3. 數(shù)據(jù)查詢(列表)

list, err := db.GetAll(ctx, "select * from user limit 2")
list, err := db.GetAll(ctx, "select * from user where age > ? and name like ?", g.Slice{18, "%john%"})
list, err := db.GetAll(ctx, "select * from user where status=?", g.Slice{1})

4. 數(shù)據(jù)查詢(單條)

one, err := db.GetOne(ctx, "select * from user limit 2")
one, err := db.GetOne(ctx, "select * from user where uid=1000")
one, err := db.GetOne(ctx, "select * from user where uid=?", 1000)
one, err := db.GetOne(ctx, "select * from user where uid=?", g.Slice{1000})

5. 數(shù)據(jù)保存

r, err := db.Save(ctx, "user", gdb.Map {
    "uid"  :  1,
    "name" : "john",
})

6. 批量操作

其中?batch?參數(shù)用于指定批量操作中分批寫入條數(shù)數(shù)量(默認(rèn)是?10?)。

_, err := db.Insert(ctx, "user", gdb.List {
    {"name": "john_1"},
    {"name": "john_2"},
    {"name": "john_3"},
    {"name": "john_4"},
}, 10)

7. 數(shù)據(jù)更新/刪除

// db.Update/db.Delete 同理
// UPDATE `user` SET `name`='john' WHERE `uid`=10000
r, err := db.Update(ctx, "user", gdb.Map {"name": "john"}, "uid=?", 10000)
// UPDATE `user` SET `name`='john' WHERE `uid`=10000
r, err := db.Update(ctx, "user", "name='john'", "uid=10000")
// UPDATE `user` SET `name`='john' WHERE `uid`=10000
r, err := db.Update(ctx, "user", "name=?", "uid=?", "john", 10000)

注意,參數(shù)域支持并建議使用預(yù)處理模式(使用???占位符)進(jìn)行輸入,避免?SQL?注入風(fēng)險。


本文標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame數(shù)據(jù)庫ORM-方法操作
本文URL:http://uogjgqi.cn/article/cdedopp.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流