web Development is basically inseparable from accessing databases , stay Gin Use in mysql Databases need to rely on mysql Drive of . Use the driver directly API I have to write a lot of template code . You can find a lot of extension packages here jmoiron/sqlx. There is also a package for handling null values guregu/null. ``` go get github.com/go-sql-driver/mysql go get gopkg.in/guregu/null.v4 go get github.com/jmoiron/sqlx ``` ### Connect to the database jmoiron/sqlx Package for ```database/sql``` There are many ways to extend the suite , for example Select You can map query results directly to structures , There is no need to bind every column . Use jmoiron/sqlx How to connect to the database and mysql The driver provides the same approach , You can call directly sqlx.Connect And pass in the connection string . Here we use go Language init Mechanism initializes the database connection . ```golang package db import ( _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "log" ) var Db *sqlx.DB func init() { db, err := sqlx.Connect("mysql", "...?parseTime=true") if err != nil { log.Panicln("db err: ", err.Error()) } db.SetMaxOpenConns(20) db.SetMaxIdleConns(20) Db = db } ``` Set in the connection string parseTime=true It's to analyze mysql Middle date time type . ```golang type SysRole struct { Id int64 `json:"id"` Name sql.NullString `json:"name"` // The role of Description sql.NullString `json:"description"` Available sql.NullInt32 `json:"available"` CreateTime sql.NullTime `json:"create_time" db:"create_time"` // New time UpdateTime sql.NullTime `json:"update_time" db:"update_time"` // Update time } func main() { r := gin.Default() r.GET("/test", func(c *gin.Context) { var sysRole []SysRole dataSql := ` select id, name, description, available, create_time, update_time from sys_role ` err := db.Db.Select(&sysRole, dataSql) if err != nil { panic(`select sys_role err: ` + err.Error()) } c.JSON(200, gin.H{ "data": sysRole, }) }) r.Run(":9001") } ``` stay go In language int、string Such types cannot be empty ,sql.NullXXX Type represents the nullable type in the database . And pay attention to CreateTime The label of the field , If the column name of the table is different from the column name of the structure, you need to add db Label ```db:"create_time"```. Visit the interface and you will find that the result may not be what you want , Every nullable field becomes an object . ```json { "data": [ { "id": 1, "name": { "String": "role:root", "Valid": true }, "description": { "String": " Super Administrator ", "Valid": true }, "available": { "Int32": 1, "Valid": true }, "create_time": { "Time": "2020-10-25T03:13:12Z", "Valid": true }, "update_time": { "Time": "2020-10-25T03:13:12Z", "Valid": true } } ... ] } ``` ### Solve the problem of null value Use guregu/null Packages can solve the problem of null values ,guregu/null For databases and JSON Provides a nullable data type , You can replace everything sql.NullXXX Type . The updated structure is as follows : ```golang type SysRole struct { Id int64 `json:"id"` Name null.String `json:"name"` // The role of Description null.String `json:"description"` Available null.Int `json:"available"` CreateTime null.Time `json:"create_time" db:"create_time"` // New time UpdateTime null.Time `json:"update_time" db:"update_time"` // Update time } ``` Visit the interface again and you will see the familiar JSON It turns out , And it can return the correct value when the data is empty . ```json { "data": [ { "id": 1, "name": "role:root", "description": " Super Administrator ", "available": 1, "create_time": "2020-10-25T03:13:12Z", "update_time": null }, ... ] } ``` The source of the article :[ Based on gin Of golang web Develop : Visit mysql Database ][source] [source]: https://www.huaface.com/arti