新聞中心
這里有您想知道的互聯網營銷解決方案
創(chuàng)新互聯GoFrame教程:GoFrame數據庫ORM-方法操作
方法操作
方法操作用于原生?SQL?執(zhí)行,相對鏈式操作更偏底層操作一些,在?ORM?鏈式操作執(zhí)行不了太過于復雜的?SQL?操作時,可以交給方法操作來處理。

成都創(chuàng)新互聯公司2013年開創(chuàng)至今,先為廣漢等服務建站,廣漢等地企業(yè),進行企業(yè)商務咨詢服務。為廣漢企業(yè)網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
接口文檔: https://pkg.GO.dev/github.com/gogf/gf/v2/database/gdb
常用方法:
本文檔的方法列表可能滯后于于代碼,詳細的方法列表請查看接口文檔,以下方法僅供參考。
// SQL操作方法,返回原生的標準庫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)
// 數據表記錄查詢:
// 查詢單條記錄、查詢多條記錄、獲取記錄對象、查詢單個字段值(鏈式操作同理)
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
// 數據單條操作
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)
// 數據修改/刪除
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)簡要說明:
- ?
Query?是原始的數據查詢方法,返回的是原生的標準庫的結果集對象,需要自行解析。推薦使用?Get*?方法,會對結果自動做解析。 - ?
Exec?方法用于寫入/更新的?SQL?的操作。 - 在執(zhí)行數據查詢時推薦使用?
Get*?系列查詢方法。 - ?
Insert/Replace/Save?方法中的?data?參數支持的數據類型為:?string/map/slice/struct/*struct?,當傳遞為?slice?類型時,自動識別為批量操作,此時?batch?參數有效。
操作示例
1. ORM對象
// 獲取默認配置的數據庫對象(配置名稱為"default")
db := g.DB()
// 獲取配置分組名稱為"user-center"的數據庫對象
db := g.DB("user-center")
// 使用原生單例管理方法獲取數據庫對象單例
db, err := gdb.Instance()
db, err := gdb.Instance("user-center")
// 注意不用的時候不需要使用Close方法關閉數據庫連接(并且gdb也沒有提供Close方法),
// 數據庫引擎底層采用了鏈接池設計,當鏈接不再使用時會自動關閉
2. 數據寫入
r, err := db.Insert(ctx, "user", gdb.Map {
"name": "john",
})
3. 數據查詢(列表)
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. 數據查詢(單條)
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. 數據保存
r, err := db.Save(ctx, "user", gdb.Map {
"uid" : 1,
"name" : "john",
})
6. 批量操作
其中?batch?參數用于指定批量操作中分批寫入條數數量(默認是?10?)。
_, err := db.Insert(ctx, "user", gdb.List {
{"name": "john_1"},
{"name": "john_2"},
{"name": "john_3"},
{"name": "john_4"},
}, 10)
7. 數據更新/刪除
// 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)注意,參數域支持并建議使用預處理模式(使用???占位符)進行輸入,避免?SQL?注入風險。
分享文章:創(chuàng)新互聯GoFrame教程:GoFrame數據庫ORM-方法操作
URL標題:http://m.fisionsoft.com.cn/article/cdedopp.html


咨詢
建站咨詢
