Mysql
go-zero
provides easier operation of mysql
API.
tip
But stores/mysql
positioning is not an orm
framework. If you need to generate model
layer code through sql/scheme
-> model/struct
reverse engineering, developers can use goctl model, this is an excellent feature.
#
Features- Provides a more developer-friendly API compared to native
- Complete the automatic assignment of
queryField -> struct
- Insert "bulkinserter" in batches
- Comes with fuse
- API has been continuously tested by several services
- Provide
partial assignment
feature, do not force strict assignment ofstruct
#
ConnectionLet's use an example to briefly explain how to create a mysql
connected model:
#
CRUDPrepare an User model
Among them, userBuilderQueryRows
: go-zero
provides struct -> [field...]
conversion. Developers can use this as a template directly.
#
insert- Splicing
insertsql
- Pass in
insertsql
and thestruct field
corresponding to the placeholder ->con.Exex(insertsql, field...)
caution
conn.Exec(sql, args...)
: args...
needs to correspond to the placeholder in sql
. Otherwise, there will be problems with assignment exceptions.
go-zero
unified and abstracted operations involving mysql
modification as Exec()
. So the insert/update/delete
operations are essentially the same. For the remaining two operations, the developer can try the above insert
process.
#
queryYou only need to pass in the querysql
and model
structure, and you can get the assigned model
. No need for developers to manually assign values.
- Declare
model struct
, splicingquerysql
conn.QueryRow(&model, querysql, args...)
:args...
corresponds to the placeholder inquerysql
.
caution
The first parameter in QueryRow()
needs to be passed in Ptr
"The bottom layer needs to be reflected to assign a value to struct
"
The above is to query one record, if you need to query multiple records, you can use conn.QueryRows()
The difference from QueryRow()
is: model
needs to be set to Slice
, because it is to query multiple rows, and multiple model
s need to be assigned. But at the same time you need to pay attention to ️: the first parameter needs to be passed in Ptr
#
querypartialIn terms of use, it is no different from the above-mentioned QueryRow()
, "this reflects the highly abstract design of go-zero
."
the difference:
QueryRow()
:len(querysql fields) == len(struct)
, and one-to-one correspondenceQueryRowPartial()
:len(querysql fields) <= len(struct)
numA: Number of database fields; numB: the number of defined struct
attributes.
If numA <numB
, but you just need to unify multiple queries, "multiple struct
is defined to return different purposes, and all of them can use the same querysql
", you can use QueryRowPartial()
#
TransactionTo perform a series of operations in a transaction, the general process is as follows:
As in the above example, developers only need to wrap all operations in transaction in a function func(session sqlx.Session) error {}
, if the operation in the transaction returns any error, Transact( )
will automatically roll back the transaction.
#
Distributed transactionsgo-zero has deeply cooperated with dtm and has natively supported distributed transactions, see distributed-transaction for details