当前位置:网站首页>Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
2022-07-27 07:11:00 【Tie Zhu】
One 、 Preface
This is the notes before moving , Just used golang When , notice mysql The packaging part of , Always curious about why so many bags are used , for example :
"database/sql"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx"
"vitess.io/vitess/go/vt/sqlparser"
"github.com/Masterminds/squirrel"
"github.com/lann/builder"
What's the difference between these bags ? What exactly are we packaging mysql Key packages needed ?
Two 、 Parse various packages
Reference resources :Go Language standard library learning database/sql—— Database management tools
1、database/sql
sql Bag is go Officially provided operation mysql An extension of , It mainly provides operation mysql Methods , For example, add, delete, change and check , Functions such as transaction .
sql The main way of operation depends on native sql To write , for example :
db.Query("SELECT * FROM student")
2、go-sql-driver/mysql"
This bag is also go Official mysql Drive pack . Connect mysql The steps are as follows :
1. Import database engine , That is to say, import mysql drive
2. Connect to database : root:[email protected]tcp(host:port)/database?charset=utf8&parseTime=True&loc=Local
3. Set related parameters
4. Test connection
The driver package here , Mainly import mysql drive , Let's have sql Packages can connect to databases , The main usage is as follows :
_ "github.com/go-sql-driver/mysql"
db, err := sql.Open("mysql",config) // The first parameter mysql It's usage
Use “_” The operation refers to the package, and the export function in the package cannot be called through the package name , But just to simply call it init() function . After importing the database engine , You can go through sql Bag sql.open() Method to connect to the database .
3、sqlx package
Reference resources :
sqlx file
sqlx Instructions
sqlx yes Go A package , It's built in with excellent database/sql A set of extensions are provided on the package . in other words .sqlx It is also an operation mysql The standard library of .
since sql The bag has been officially issued , So why do people prefer to use sqlx Bao Er ?
(1) Security
database/sql No validation of the query text will be attempted ; It is sent to the server as is with the encoding parameters .
and sqlx Special parameter binding methods are provided sqlx.DB.Rebind(string) string, Support mysql question mark (?)
Or named Prepared Statements, avoid SQL Injected security issues
(2) facilitate
Relative to native sql Bag query,sqlx.Rowx The main extension of is StructScan, It can automatically scan the results
To the domain in the corresponding structure (fileld) in . hold SQL The result set of execution is transformed into a data structure (Struct、Maps、Slices).
(3)sqlx Of get and select Method
Get and Select It's a very time-saving extension . They put query And very flexible scan Combine grammar .
Get and Select It will close after executing the query Rows, And any problem encountered during the execution phase will return an error .
shortcoming :Select The whole result will be put into memory at one time . If the query result is not limited to a specific size , So it's best to use Query/StructScan Iterative method .
sqlx Bag sql Mode of operation , Is also dependent on native sql To write :
db.Query("SELECT * FROM student")
4、reflectx package
sqlx Packages have special reflection requirements . especially , It needs to :
Ability to map names to fields
Understand the embedded structure
Learn how to map names to fields with specific tags
User specified name -> Field mapping function
reflectx The package mainly extends the original reflect package , To achieve the above purpose .
5、squirrel package and Builder package
The main functions of these two packages are sql generator , Realize query construction , For chained calls . We know the original sql Packages need to be written manually sql Of , Programmers like to be lazy , It is best to input the initial letter and it can be completed automatically , Little by little chain calls all the way to the end .
(1)sqlx + squirrel Realization
squirrel yes go Of sql generator , Similar to the query constructor used before , Chain call can be implemented , Such as :
sql, args, err := sq.
Insert("users").Columns("name", "age").
Values("moe", 13).Values("larry", sq.Expr("? + 5", 12)).
ToSql()
(2)builder package
Builder Originally for Squirrel Compiling , It is a smooth SQL generator . This may be Builder Best example of .
Builder The method chain helps you write fluent DSL
6、vitess.io/vitess/go/vt/sqlparser package
Be careful : This address is now 404 了
(1) vitess-sqlparser
vitess-sqlparser just Go Language SQL and DDL Parser ( from vitess and TiDB Support )
(2)vitess
Reference resources :https://blog.51cto.com/u_15080030/2642755
Vitess Is a tool for deploying 、 Expand and manage large mysql The database solution of instance cluster .
The above understanding of the common mysql Related to the package , No wonder the big guys sealed mysql You need to introduce so many packages when you need to , Interlocking , From security to chain call , The combination and packaging make it easier for everyone to use .
3、 ... and 、sqlx and gorm
1.gorm package
Reference resources :Gorm Official documents
GORM yes GoLang The best in ORM frame , Support MySQL、PostgreSQL、Sqlite、SQL Server, The function is very
Powerful , It can also be executed directly SQL And get the result set . The main operation mode is orm In the form of , be relative to sql For expansion packs ,
orm Is a deeper level of packaging , For simplicity sql, Use orm It can effectively improve efficiency .
orm yes object Relational Mapping abbreviation ,object It's the object ,relational It's the relationship ,mapping Mapping .
Object relational mapping looks quite abstract , you 're right , It is an abstract concept . From the perspective of programming syntax ,
It is in the specific operation of business objects , There is no need to go and complex SQL Deal with sentences , Just a simple
Manipulate the properties and methods of the object .
gorm It is also similar to other languages ORM How to operate the database , More inclined to all objects .
2、gorm and sqlx Comparison of
(1)sqlx Is a library , It allows you to scan the entire line into your structural variables . although sqlx Reduced for building CRUD And the number of typical lines written , But eventually you will still write duplicate code many times .
Use ORM Can help reduce it and focus on your business logic .
(2) For complex sq, For example, write sub queries of reports , Multi table association operation ,orm The implementation may be more complex , Use native sql It's better .
(3) Efficiency
Reference resources :https://tech.wmzhe.com/article/97958.html , obviously sqlx More efficient
(4)orm Is a class mapped to a table , Fields are mapped to member variables . When there are many fields in the table ,orm Classes will be very verbose .
It doesn't matter sqlx still gorm, It is enough for our daily development , Bloggers have stayed in several companies with sqlx Mainly ,ORM Although convenient , But more abstract , Maybe you think sqlx It's easier to get started .
Bloggers think , The important thing is not to use sqlx Or use it gorm, It is important to unify standards and sql Encapsulation of layers . Unified standards are for the readability and maintainability of code ,sql Layer encapsulation is to prevent code redundancy and facilitate the insertion of onion model middleware , Achieve relevant indicators of service stability .
3、 encapsulation sql What are the concerns
(1) Read the central configuration , Connect mysql, such as apollo etc.
(2) Add onion model , Such as adding trace,spanID,requestId Evenly distributed links are traced to context,
add to log Print key logs
end
边栏推荐
- DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针
- 把Excel转换成CSV/CSV UTF-8
- The issuing process of individual developers applying for code signing certificates
- Interpretation of deepsort source code (IV)
- pre-commit install 时 CalledProcessError
- 含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
- 肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA
- Express receive request parameters
- Music website management system based on SSM
- DNA modified zinc oxide | DNA modified gold nanoparticles | DNA coupled modified carbon nanomaterials
猜你喜欢

肽核酸PNA-多肽PNA-TPP|Glt-Ala-Ala-Pro-Leu-pNA|Suc-Ala-Pro-pNA|Suc-AAPL-pNA|Suc-AAPM-pNA

Cyclegan parsing

PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA

Student achievement management system based on SSM

Watermelon book learning notes - Chapter 4 decision tree

Cass11.0.0.4 for autocad2010-2023 dog free usage

CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法

工控用Web组态软件比组态软件更高效

大疆livox定制的格式CustomMsg格式转换pointcloud2

手机上也能训练BERT和ResNet了?!
随机推荐
The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.
2021 interview questions for php+go of Zhongda factory (2)
Watermelon book learning notes - Chapter 1 and 2
Analysis of online and offline integration mode of o2o E-commerce
How to learn C language? This article gives you the complete answer
CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法
Details of cross entropy loss function in pytorch
Common problems in converting pytorch to onnx
DNA科研实验应用|环糊精修饰核酸CD-RNA/DNA|环糊精核酸探针/量子点核酸探针
Peptide nucleic acid oligomer containing azobenzene monomer (nh2-tnt4, n-pnas) Qiyue biological customization
word-wrap:break-word 换行在各浏览器兼容的问题
Two ways of multi GPU training of pytorch
Livox SLAM(带LIO+闭环检测优化)
AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition
DNA(脱氧核糖核酸)供应|碳纳米管载核酸-DNA/RNA材料|DNA/RNA核酸修饰磁性纳米颗粒
Derivative, partial derivative and gradient
从技术原理看元宇宙的可能性:Omniverse如何“造”火星
Drools(5):Drools高级语法
Analysis on the current situation and optimization strategy of customer experience management in banking industry
Digital image processing Chapter 1 Introduction