Gorm-Plus is a Gorm enhancement tool. It provides developers with enhancements out of the box while maintaining the original features of Gorm. By simplifying the development process and improving efficiency, it brings an unparalleled development experience to developers. If you are eager to try a tool that makes development easy and efficient, Gorm-Plus will be an option you can’t miss.
There is currently a Users table with the following table structure:
CREATE TABLE ` users ` (
` id ` bigint NOT NULL AUTO_INCREMENT,
` username ` varchar ( 50 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL ,
` password ` varchar ( 50 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL ,
` address ` varchar ( 50 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL ,
` age ` bigint DEFAULT NULL ,
` phone ` varchar ( 11 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL ,
` score ` bigint DEFAULT NULL ,
` dept ` varchar ( 20 ) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL ,
` created_at ` datetime( 3 ) DEFAULT NULL ,
` updated_at ` datetime( 3 ) DEFAULT NULL ,
PRIMARY KEY ( ` id ` )
) ENGINE = InnoDB AUTO_INCREMENT = 407 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;
The corresponding data are as follows:
INSERT INTO ` users ` ( ` username ` , ` password ` , ` address ` , ` age ` , ` phone ` , ` score ` , ` dept ` , ` created_at ` , ` updated_at ` )
VALUES
( '张三' , ' password1 ' , '地址1 ' , 25 , ' 12345678901 ' , 80 , '部门1 ' , NOW(), NOW()),
( '李四' , ' password2 ' , '地址2 ' , 30 , ' 12345678902 ' , 90 , '部门2 ' , NOW(), NOW()),
( '王五' , ' password3 ' , '地址3 ' , 35 , ' 12345678903 ' , 70 , '部门1 ' , NOW(), NOW()),
( '赵六' , ' password4 ' , '地址4 ' , 28 , ' 12345678904 ' , 85 , '部门2 ' , NOW(), NOW()),
( '钱七' , ' password5 ' , '地址5 ' , 32 , ' 12345678905 ' , 75 , '部门1 ' , NOW(), NOW());
Download Gorm-Plus
go get github . com / acmestack / gorm - plus package main
import (
"github.com/acmestack/gorm-plus/gplus"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"time"
)
type User struct {
ID int64
Username string
Password string
Address string
Age int
Phone string
Score int
Dept string
CreatedAt time. Time
UpdatedAt time. Time
}
var gormDb * gorm. DB
func init () {
dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
var err error
gormDb , err = gorm . Open ( mysql . Open ( dsn ), & gorm. Config {
Logger : logger . Default . LogMode ( logger . Info ),
})
if err != nil {
log . Println ( err )
}
// 初始化gplus
gplus . Init ( gormDb )
}
func main () {
users , resultDb := gplus. SelectList [ User ]( nil )
log . Println ( "error:" , resultDb . Error )
log . Println ( "RowsAffected:" , resultDb . RowsAffected )
for _ , user := range users {
log . Println ( "user:" , user )
}
}Console output:
2023 / 06 / 01 17 : 48 : 19 error : < nil >
2023 / 06 / 01 17 : 48 : 19 RowsAffected : 5
2023 / 06 / 01 17 : 48 : 19 user : & { 1 张三 password1 地址1 25 12345678901 80 部门1 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST }
2023 / 06 / 01 17 : 48 : 19 user : & { 2 李四 password2 地址2 30 12345678902 90 部门2 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST }
2023 / 06 / 01 17 : 48 : 19 user : & { 3 王五 password3 地址3 35 12345678903 70 部门1 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST }
2023 / 06 / 01 17 : 48 : 19 user : & { 4 赵六 password4 地址4 28 12345678904 85 部门2 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST }
2023 / 06 / 01 17 : 48 : 19 user : & { 5 钱七 password5 地址5 32 12345678905 75 部门1 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST 2023 - 06 - 01 17 : 48 : 11 + 0 800 CST }All the query functions of a single table are completed by just one line of code.
gplus.SelectList(gplus.BuildQuery[User](queryParams))example:
func main () {
http.HandleFunc( " / " , handleRequest)
http.ListenAndServe( " :8080 " , nil)
}
func handleRequest(w http.ResponseWriter, r * http.Request) {
queryParams := r.URL.Query ()
list, _ := gplus.SelectList(gplus.BuildQuery[User](queryParams))
marshal, _ := json.Marshal(list)
w.Write(marshal)
}Suppose we want to query the user whose username is zhangsan
http://localhost:8080 ? q=username=zhangsanSuppose we want to query the username username zhang
http://localhost:8080 ? q=username~ > =zhangSuppose we want to query users with age greater than 20
http://localhost:8080 ? q=age > 20Suppose we want to query users whose username is equal to zhagnsan and password is equal to 123456
http://localhost:8080 ? q=username=zhangsan & q=password=123456Suppose we want to query users whose username is equal to zhagnsan and password is equal to 123456
http://localhost:8080 ? q=username=zhangsan & q=password=123456Suppose we want to query the user whose username is equal to zhagnsan, or usename is equal to lisi
You can add a grouping and gcond conditional query to implement it
http://localhost:8080 ? q=A.username=zhangsan & q=B.username=lisi & gcond=A | BWe only need one line of code for all single table queries.
From the above steps, we can see that integrating Gorm-Plus is very simple. Just add a line of code after initializing Gorm gplus.Init(gormDb)
It is ready to use. Not only that, it is also easy to use Gorm-Plus , and list query can be completed with just one line of code.
However, the power of Gorm-Plus is much more than that.
For more documents, please check: https://github.com/acmestack/gorm-plus/wiki
Add WeChat: afumubit, note: gplus