当前位置:网站首页>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.go
The following line of logs must be placed in model struct First row directly above .
// gen:qs
4、 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
边栏推荐
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
- SystemServer进程
- How to set QT manual layout
- Browser driven Download
- Android kotlin fragment technology point
- OpenFOAM:lduMatrix&lduAddressing
- Explanation: here is your UFO, Goldbach conjecture
- 千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?
- 默认插槽,具名插槽,作用域插槽
- 当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
猜你喜欢
Find love for speed in F1 delta time Grand Prix
BeanUtils -- shallow copy -- example / principle
混沌工程平台 ChaosBlade-Box 新版重磅发布
你的 Sleep 服务会梦到服务网格外的 bookinfo 吗
Development skills of rxjs observable custom operator
【文档树、设置】字体变小
代码实现MNLM
Selenium element positioning method
Whole house Wi Fi: a pain point that no one can solve?
[deep learning] simple implementation of neural network forward propagation
随机推荐
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
Achievements in science and Technology (27)
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
Integral link, inertia link and proportion link in Simulink
如何设置Qt手工布局
瀏覽器驅動的下載
Systemserver process
默认插槽,具名插槽,作用域插槽
P3807 [template] Lucas theorem /lucas theorem
Story point vs. Human Sky
QT how to set fixed size
Mysql5.7 installation super easy tutorial
Solution: Compression Technology (original version and sequel version)
Dingtalk 发送消息
Dingtalk send message
Unity small map production [2]
Data consistency between redis and database
Selenium, element operation and browser operation methods
Qt如何设置固定大小
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves