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

What is the super-easy to use orm library gorm?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

What is the super-easy-to-use orm library gorm? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In fact, in order to improve the efficiency of development, some orm frameworks are generally used to shield the database layer, so that users can only see objects without us to do some manual conversion, so it is very convenient to use. This mode of operation has basically become standard practice. Golang also has many excellent orm frameworks, so today we will introduce gorm.

The function of Gorm

Hook mechanism (Before/After Create/Save/Update/Delete/Find)

Object relation Has One, Has Many, Belongs To, Many To Many, Polymorphism

Thermal loading

Support for native sql operations

Transactional nature

Chain api

Supported databases are: mysql, postgre, sqlite, sqlserver

Query operation

These are the functions of gorm, but why gorm? How is gorm different from other frameworks? There are no more introductions here. Let's go straight to usage.

Library installation go get-u github.com/jinzhu/gorm

Database connection db, err = gorm.Open ("mysql", "root:root@tcp (127.0.0.1 gorm.Open 3306) / irisapp?charset=utf8&parseTime=True&loc=Local") if err! = nil {panic ("connection to database failed")}

The connection is relatively simple, just call gorm.Open directly and pass in the database address. Gorm supports almost all major relational databases, but the connection method is slightly different. Here I use mysql as an example.

Table definition type Product struct {ID int `gorm: "primary_key" `Code string `gorm: "type:varchar (20);" `Price int `gorm: "type:int;" `Name string `gorm: "type:varchar (64);" `Mail string `gorm: "type:varchar (256) "`CreatedAt time.Time} create table if! db.HasTable (& Like {}) {if err: = db.Set (" gorm:table_options "," ENGINE=InnoDB DEFAULT CHARSET=utf8 ") .CreateTable (& Product {}) .error; err! = nil {panic (err)}}

You can create tables directly through db.CreateTable, which is very convenient, and you can also set some additional table properties through db.Set.

In addition, there is a way to create tables synchronously automatically:

/ / automatic migration mode db.AutoMigrate (& Product {}) query var product Productdb.First (& product, 1) / query productdb.First (& product, "code =?", "ik01001") / query product with code 11212 insert / / create db.Create (& Product {Code: "ik01001", Price: 1000})

To construct a given object, you can insert a record by calling db.Create () directly. It is convenient not to concatenate sql statements.

Update / / Update-the price for update product is 2000db.Model (& product) .Update ("Price", 2000) delete

Simple object deletion:

Deletion of db.Delete (& product) complex conditions: if err: = db.Where (& Product {ID: 1}) .Delete (Product {}) .error; err! = nil {return err} transaction func CreateProducts (db * gorm.DB) err {tx: = db.Begin () / Note: once you are in a transaction, use tx as the database handle

If err: = tx.Create (& Product {Code: "ik01003", Price: 3000}) .error; err! = nil {tx.Rollback () return err}

Tx.Commit () return nil}

Transaction handling is also very simple. Start the transaction with a db.Begin () declaration, call tx.Commit () when it ends, and call tx.Rollback () when an exception occurs.

After reading the above, have you mastered the method of the super-easy to use orm library gorm? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report