SoFunction
Updated on 2025-03-05

GoFrame framework ORM native method object operation unboxing experience

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.

Previous articleOrganized the native method of ORM, This article organizes the corresponding unboxing experience based on the native method of sorting.

Unboxing experience

1. ORM object

// Get the default configured database object (configuration name is "default")db := ()
// Get the database object with the configuration group name "user"db := ("user")
// Use native singleton management method to obtain database object singletondb, err := ()
db, err := ("user")

tips

When not in use, you do not need to use the Close method to close the database connection. The underlying database engine adopts a link pool design, and it will be automatically closed when the link is no longer in use.

2. Data writing

r, err := (ctx, "user",  {
    "name": "Wang Zhongyang",
})

3. Data query (single)

Although limit 10, because we are using GetOne, it will still only return a single piece of data

one, err := (ctx, "select * from user limit 10")
one, err := (ctx, "select * from user where id=100")
one, err := (ctx, "select * from user where id=?", 100)
one, err := (ctx, "select * from user where id=?", {100})

4. Data query (list)

list, err := (ctx, "select * from user limit 10")
list, err := (ctx, "select * from user where age > ? and name like ?", {20, "%gold%"})
list, err := (ctx, "select * from user where status=?", {1})

5. Data saving

Data saving is Save, insert data is insert, the difference is whether to specify the primary key

r, err := (ctx, "user",  {
    "id"  :  1,
    "name" : "Wang Zhongyang",
})

6. Batch operation

batch := 10
_, err := (ctx, "user",  {
    {"name": "Xiao Ming"},
    {"name": "Xiaohua"},
    {"name": "Little Army"},
    {"name": "Xiao Gao"},
}, batch)

tips

  • The batch parameter is used to specify the number of batch writes in batch operations (default is 10).
  • It represents the number of batches of data when batch writes. This value is related to the SQL BufferSize value configured by your database server.

7. Data update/delete

// / Similarly// UPDATE `user` SET `name`='Wang Zhongyang' WHERE `id`=10000r, err := (ctx, "user",  {"name": "Wang Zhongyang"}, "id=?", 10000)
// UPDATE `user` SET `name`='Wang Zhongyang' WHERE `id`=10000r, err := (ctx, "user", "name='Wang Zhongyang'", "id=10000")
// UPDATE `user` SET `name`='Wang Zhongyang' WHERE `id`=10000r, err := (ctx, "user", "name=?", "id=?", "Wang Zhongyang", 10000)

tips

It is recommended to use a placeholder for input parameters to avoid the risk of SQL injection.

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 unboxing experience of the ORM native method object operation of the GoFrame framework. For more information about the operation of the ORM native method object, please pay attention to my other related articles!