Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate gorm into go-zero

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to integrate gorm into go-zero". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to integrate gorm into go-zero.

The code generator provided by go-zero does not provide orm framework operations, but does provide traversal caching operations. But gorm framework, there is no better cache plug-in, although there is a gcache, but does not support the gorm2.0 version.

So I'm going to combine the two. An interface is mentioned in the gorm official documentation to get the generated sql statements.

So you can use gorm as a generator of sql statements, and put the generated sql statements into the template generated by go-zero to execute.

Sql Generator in gorm

Stmt: = DB.Session (& Session {DryRun: true}). First (& user, 1). Statementstmt.SQL.String () / = > SELECT * FROM `users`WHERE `id` = $1 ORDER BY `id`stmt.Vars / / = > [] interface {} {1}

The effect of integration into go-zero is as follows

Call GeneralSQL, and after generating the sql using gorm, execute

Func (m * defaultJojUserModel) FindOne (id int64) (* JojUser, error) {jojUsercenterJojUserIdKey: = fmt.Sprintf ("% s% v", cacheJojUsercenterJojUserIdPrefix, id) var resp JojUser err: = m.QueryRow (& resp, jojUsercenterJojUserIdKey, func (conn sqlx.SqlConn, v interface {}) error {query, values: = m.GeneralSQL (func (tx * gorm.DB) * gorm.DB {return tx.First (& resp) Id)}) return conn.QueryRow (v, query, values...)}) switch err {case nil: return & resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil Err}} func (m * defaultJojUserModel) GeneralSQL (queryFn func (tx * gorm.DB) * gorm.DB) (string, [] interface {}) {tx: = queryFn (& gorm.Session {DryRun: true}) stmt: = tx.Statement return stmt.SQL.String (), stmt.Vars}

On the question of efficiency, a simple test is done. If you use gorm to generate sql, it takes time to generate 1e5 304.1878ms.

Type ArticleComment struct {Id int `json: "id" `Content string `json: "content" `ReplyComment * ArticleComment `json: "replyToComment" `/ / comments replied to this comment ParentCommentId int `json: "parentCommentId"` / / parent comment Id ChildComment [] ArticleComment `json: "childComment" `/ / with the Comment on all comments without parent node CreateTime string `json: "createTime" `/ / reply time} func main () {begin: = time.Now () for I: = 0 I < 100000 ITunes + {GeneralSQL (func (tx * gorm.DB) * gorm.DB {return tx.Find (& Article {}, [] int {1,2})} end: = time.Now () fmt.Println (end.Sub (begin)) func GeneralSQL (queryFn func (tx * gorm.DB) * gorm.DB) (string [] interface {}) {tx: = queryFn (db.Session (& gorm.Session {DryRun: true})) stmt: = tx.Statement return stmt.SQL.String (), stmt.Vars so far I believe you have a deeper understanding of "how to integrate gorm into go-zero". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report