当前位置:网站首页>Golang quickly generates model and queryset of database tables
Golang quickly generates model and queryset of database tables
2022-07-02 14:07:00 【It workers】
It's generation model The directory xxx.go and xxx_gen.go file
Tools used :
Quickly generate model:gentool
according to model Generate queryset:go-queryset
Here are the detailed steps .
summary
Golang When operating database tables , You may often need to create a corresponding data table as shown in the following figure struct object , Is often referred to as model, Some of you might say ,model There are few fields , Just write it by hand , There is no need to use tools to generate . When there are fewer database tables , And when there are few fields in the table, it can be done by hand , But if you need to generate model There are many tables in , Every model There are also many fields , At this time, it may be a more appropriate choice to use tools to generate , It's also less likely to make mistakes . So here's an example of how to quickly generate model Document and queryset Methods .
Install and download first gentool Tools
go install gorm.io/gen/tools/[email protected]
Execute the following command to generate... Corresponding to the data table model file , For the usage of the command, refer to the following “ Case presentation ”.
gentool -dsn "user:[email protected](127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "student" -onlyModel
Case presentation
1、 Generate model file
For example, I want to give student Table to generate model ,student The table has the following fields .
CREATE TABLE `student`(
`id` BIGINT(20) UNSIGNED not NULL AUTO_INCREMENT COMMENT ' Self increasing id',
`school_id` BIGINT(20) NOT NULL DEFAULT '0' COMMENT ' Student number ',
`name` VARCHAR(64) NOT NULL DEFAULT '0' COMMENT ' full name ',
`male` TINYINT(4) NOT NULL DEFAULT '0' COMMENT ' Gender ',
`phone_number` BIGINT(20) NOT NULL DEFAULT '0' COMMENT ' cell-phone number ',
`addr` VARCHAR(255) NOT NULL DEFAULT '' COMMENT ' Home address ',
`class` VARCHAR(255) NOT NULL DEFAULT '0' COMMENT ' class ',
`major` INT NOT NULL DEFAULT '0' COMMENT ' major id',
`grade` INT NOT NULL DEFAULT '0' COMMENT ' Year of enrollment / grade ',
`extra` text NOT NULL DEFAULT '' COMMENT ' other ',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Creation time ',
`modify_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ' Update time ',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT=' Student basic information table ';Use the following command to generate model,model The file will be generated one level above the current path called model In the folder of , For example demo/aaa Execute the above command under , that model Will be generated in demo/model Under the path , If model If the path does not exist, it will be created automatically .
gentool -dsn "root:[email protected](127.0.0.1:3306)/my_test_db?charset=utf8mb4&parseTime=True&loc=Local" -tables "student" -onlyModel
After the command is executed , Will find demo The path generates A group called model Folder , There's one in the folder student.gen.go file , This is what we just generated model The file
If you want to specify model Path to file , Need to add -modelPkgName Parameters , The complete command is as follows , After execution ,model Files will be generated in aaa Under the path , We need to pay attention to -modelPkgName The path of needs to be an absolute path , It can't be a relative path .
gentool -dsn "root:[email protected](127.0.0.1:3306)/my_test_db?charset=utf8mb4&parseTime=True&loc=Local" -tables student" -onlyModel -modelPkgName="/Users/luochunhui/go/src/demo/aaa"
2、 Change file name
We are usually used to putting student.gen.go This model In the document '.gen' String removal , That is, rename it to student.go
3、model Add necessary comments to the file
Next we need to use model File generation queryset file , That is what we often see '_gen.go' Final document , Generate queryset Before document , Need to be in model Add... To the file 2 Line notes , Although it is a comment , But these two lines of comments are essential .
student.go It is the present. model File name of the file ,student_gen.go Is to be generated queryset File name of the file
//go:generate goqueryset -in student.go -out student_gen.goThe following line of logs must be placed in model struct First row directly above .
// gen:qs4、 install goqueryset
Execute the following command to goqueryset Tool installation
go get -u github.com/jirfag/go-queryset/cmd/goqueryset
# If the top go get Command to install goqueryset Failure , You can try the following command , If you succeed, you don't have to try
go install github.com/jirfag/go-queryset/cmd/[email protected]
5、 Generate queryset file
After adding comments , Enter into model File path , Execute the following command to generate queryset file .
go generate ./...
After execution, you can see the and model A file is generated under the same level directory student_gen.go file , This file is queryset file .
6、 Last
In the generated queryset You can see github.com/jinzhu/gorm This dependency has not been installed yet , Execute the following command to pull , No error should be reported after pulling .
go get github.com/jinzhu/gorm
go mod tidy边栏推荐
- Node. JS accessing PostgreSQL database through ODBC
- 代码实现MNLM
- Code implementation MNLM
- Dingtalk 发送消息
- Qt-制作一个简单的计算器-实现四则运算
- Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
- 瀏覽器驅動的下載
- SystemServer进程
- [deep learning] simple implementation of neural network forward propagation
- Design of non main lamp: how to make intelligent lighting more "intelligent"?
猜你喜欢

默认插槽,具名插槽,作用域插槽

Code implementation MNLM

MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)

Just 1000 fans, record it

你的 Sleep 服务会梦到服务网格外的 bookinfo 吗

The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%

抓包工具fiddler学习

MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)

Drawing Nyquist diagram with MATLAB

Systemserver process
随机推荐
【文档树、设置】字体变小
每日学习3
Pointer from entry to advanced (1)
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
Golang 快速生成数据库表的 model 和 queryset
Dingtalk 发送消息
刚好1000粉丝,记录一下
Integral link, inertia link and proportion link in Simulink
路由(二)
Let juicefs help you with "remote backup"
P3008 [usaco11jan]roads and planes g (SPFA + SLF optimization)
Engineers who can't read device manuals are not good cooks
不会看器件手册的工程师不是个好厨子
给Android程序员的一些面试建议「建议收藏」
P1042 [NOIP2003 普及组] 乒乓球
How to set QT manual layout
Custom events, global event bus, message subscription and publishing, $nexttick
P1908 reverse sequence pair
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)