SoFunction
Updated on 2025-03-04

GoFrame ORM native method operation example

Preface

I have been using GoFrame (hereinafter referred to as gf) to develop projects recently. After getting familiar with business logic, I just keep typing the code.

Previous articles about structure and json conversion:GoFrame must know Scan: Type conversion, today I have compiled important ORM-related articles.

gf supports ORM native operations. When ORM chain operations cannot perform too complex SQL operations, they can be handed over to the method operations to handle them.

This article organizes common methods of native operations, and the next article organizes the corresponding unboxing experience based on the organized native methods.

Common methods

SQL operation method, return native standard library SQL object

  • Query is the original data query method, which returns the result set object of the native standard library and needs to be parsed by itself.
  • The Exec method is used to write/update SQL operations.
Query(ctx , query string, args ...interface{}) (*, error)
Exec(ctx , query string, args ...interface{}) (, error)
Prepare(ctx , query string) (*, error)

Data table record query:

  • Query a single record, query multiple records, obtain record objects, query a single field value (same as chain operations)
  • When executing data queries, use Get* series query methods are recommended.
GetAll(ctx , sql string, args ...interface{}) (Result, error)
GetOne(ctx , sql string, args ...interface{}) (Record, error)
GetValue(ctx , sql string, args ...interface{}) (Value, error)
GetArray(ctx , sql string, args ...interface{}) ([]Value, error)
GetCount(ctx , sql string, args ...interface{}) (int, error)
GetScan(ctx , objPointer interface{}, sql string, args ...interface{}) error

Single data operation

The data parameters in the Insert/Replace/Save method support data types:

string/map/slice/struct/*struct, when passed to slice type, it is automatically recognized as a batch operation. At this time, the batch parameter is valid.

Insert(ctx , table string, data interface{}, batch...int) (, error)
Replace(ctx , table string, data interface{}, batch...int) (, error)
Save(ctx , table string, data interface{}, batch...int) (, error)

Here I sincerely exclaim: gf is indeed very convenient. I still remember that when I used the gorm1.1 version, I was troubled by batch insertion and couldn't help myself:# Go GORM is time to upgrade to new version 2.0 new features introduction

Data modification/deletion

Update(ctx , table string, data interface{}, condition interface{}, args ...interface{}) (, error)
Delete(ctx , table string, condition interface{}, args ...interface{}) (, error)

Summarize

Although GoFrame's ORM chain operation is very simple and powerful, there are always some logic in the business that needs to be implemented using native methods, simplifying the complexity.

The above is the detailed content of the operation example of GoFrame ORM native method. For more information about GoFrame ORM native method, please follow my other related articles!