日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame數(shù)據(jù)庫ORM-方法操作

方法操作

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

成都創(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ù)表記錄查詢:
// 查詢單條記錄、查詢多條記錄、獲取記錄對象、查詢單個(gè)字段值(鏈?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*?方法,會(huì)對結(jié)果自動(dòng)做解析。
  2. ?Exec?方法用于寫入/更新的?SQL?的操作。
  3. 在執(zhí)行數(shù)據(jù)查詢時(shí)推薦使用?Get*?系列查詢方法。
  4. ?Insert/Replace/Save?方法中的?data?參數(shù)支持的數(shù)據(jù)類型為:?string/map/slice/struct/*struct?,當(dāng)傳遞為?slice?類型時(shí),自動(dòng)識(shí)別為批量操作,此時(shí)?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")

// 注意不用的時(shí)候不需要使用Close方法關(guān)閉數(shù)據(jù)庫連接(并且gdb也沒有提供Close方法),
// 數(shù)據(jù)庫引擎底層采用了鏈接池設(shè)計(jì),當(dāng)鏈接不再使用時(shí)會(huì)自動(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)險(xiǎn)。


網(wǎng)頁名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFrame數(shù)據(jù)庫ORM-方法操作
URL鏈接:http://www.5511xx.com/article/cdedopp.html