当前位置:网站首页>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边栏推荐
猜你喜欢

Integral link, inertia link and proportion link in Simulink

Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?

Error: eacces: permission denied, access to "/usr/lib/node_modules"

瀏覽器驅動的下載

Don't spend money, spend an hour to build your own blog website

Do you know that there is an upper limit on the size of Oracle data files?

The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!

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

Mysql5.7 installation super easy tutorial

Gee learning notes 2
随机推荐
Systemserver process
QT new project_ MyNotepad++
故事點 vs. 人天
Integral link, inertia link and proportion link in Simulink
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 04 | 深入浅出索引(上)
Selenium, element operation and browser operation methods
【文档树、设置】字体变小
SystemServer进程
Skillfully use SSH to get through the Internet restrictions
每天坚持20分钟go的基础二
默认插槽,具名插槽,作用域插槽
selenium,元素操作以及浏览器操作方法
Origin绘制热重TG和微分热重DTG曲线
Verification failed, please check your call back website. You can follow the instructions
Qt入门-制作一个简易的计算器
Dingtalk 发送消息
mysql ---- Oracle中的rownum转换成MySQL
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
Launcher启动过程