当前位置:网站首页>Golang 快速生成数据库表的 model 和 queryset
Golang 快速生成数据库表的 model 和 queryset
2022-07-02 10:32:00 【IT工作者】
就是生成 model 目录的 xxx.go 和 xxx_gen.go 文件
使用的工具:
快速生成 model:gentool
根据 model 生成 queryset:go-queryset
下面是详细操作步骤。
概述
Golang 操作数据库表时,可能经常需要创建像下图中所示的与数据表相对应的 struct 对象,常被称为 model,有同学可能会说,model 也没几个字段,手写不就好了,没必要用工具来生成。当数据库表比较少,且表的字段少时用手敲确实可以完成,但是如果需要生成model的表数量比较多,每个model的字段也比较多,这时是用工具来生成可能是个更合适的选择,也更不容易出错。所以这里介绍一个能快速生成 model 文件和 queryset 的方法。
先安装下载 gentool 工具
go install gorm.io/gen/tools/[email protected]
执行下面这一条命令即可生成与数据表相对应的 model 文件,命令的用法参考下面的“案例演示”。
gentool -dsn "user:[email protected](127.0.0.1:3306)/database?charset=utf8mb4&parseTime=True&loc=Local" -tables "student" -onlyModel
案例演示
1、生成 model 文件
比如我想给student表生成 model ,student 表有下面这些字段。
CREATE TABLE `student`(
`id` BIGINT(20) UNSIGNED not NULL AUTO_INCREMENT COMMENT '自增id',
`school_id` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '学号',
`name` VARCHAR(64) NOT NULL DEFAULT '0' COMMENT '姓名',
`male` TINYINT(4) NOT NULL DEFAULT '0' COMMENT '性别',
`phone_number` BIGINT(20) NOT NULL DEFAULT '0' COMMENT '手机号',
`addr` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '家庭住址',
`class` VARCHAR(255) NOT NULL DEFAULT '0' COMMENT '班级',
`major` INT NOT NULL DEFAULT '0' COMMENT '专业id',
`grade` INT NOT NULL DEFAULT '0' COMMENT '入学年份/年级',
`extra` text NOT NULL DEFAULT '' COMMENT '其他',
`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`modify_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='学生基本信息表';使用如下命令生成model,model文件会生成在当前路径上一级的名为model的文件夹中,比如在demo/aaa下执行上面的命令,那么 model 会生成在 demo/model 路径下,如果 model 路径不存在会自动创建。
gentool -dsn "root:[email protected](127.0.0.1:3306)/my_test_db?charset=utf8mb4&parseTime=True&loc=Local" -tables "student" -onlyModel
命令执行完毕后,会发现在 demo 路径下生成了 一个名为 model 的文件夹,文件夹里有个 student.gen.go 文件,这个就是我们刚才生成的 model 文件了
如果想指定 model 文件的路径,需要添加 -modelPkgName 参数,完整命令如下所示,执行完毕后,model 文件将生成在 aaa 路径下,需要注意 -modelPkgName 的路径需要是绝对路径,不能是相对路径。
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、修改文件名
我们一般习惯于把 student.gen.go这个model 文件中的 '.gen' 字符串去除,也就是重命名为 student.go
3、model 文件添加必要注释
接下来需要用 model 文件生成 queryset 文件,也就是我们经常看到的以 '_gen.go' 结尾的文件,生成 queryset 文件前,需要在 model 文件中添加 2 行注释,虽然是注释,但是这两行注释必不可少。
student.go 是当前model 文件的文件名,student_gen.go 是将要生成的 queryset 文件的文件名
//go:generate goqueryset -in student.go -out student_gen.go下面这行日志必须放在model struct正上方第一行。
// gen:qs4、安装 goqueryset
执行下面的命令进行 goqueryset 工具安装
go get -u github.com/jirfag/go-queryset/cmd/goqueryset
# 如果上面的 go get 命令安装 goqueryset 失败,可以尝试下面这条命令,如果成功了就不用尝试了
go install github.com/jirfag/go-queryset/cmd/[email protected]
5、生成 queryset 文件
注释添加完毕后,进入到 model 文件所在路径,执行下面的命令来生成 queryset 文件。
go generate ./...
执行完毕可以看到与 model 文件同级目录下生成了一个 student_gen.go 文件,这个文件就是 queryset 文件。
6、最后
在生成的queryset 可以看到 github.com/jinzhu/gorm这个依赖还没安装,执行下面的命令拉取即可,拉取完毕应该就不报错了。
go get github.com/jinzhu/gorm
go mod tidy边栏推荐
- 三谈exception——错误处理
- 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
- [technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
- Why can't d link DLL
- Everyone believes that the one-stop credit platform makes the credit scenario "useful"
- Subcontracting configuration of uniapp applet subpackages
- 代码实现MNLM
- Why is the default of switch followed by break?
- I did it with two lines of code. As a result, my sister had a more ingenious way
猜你喜欢

Mysql5.7 installation super easy tutorial

Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking

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

不会看器件手册的工程师不是个好厨子

BeanUtils--浅拷贝--实例/原理

Development skills of rxjs observable custom operator

你知道Oracle的数据文件大小有上限么?

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

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

刚好1000粉丝,记录一下
随机推荐
Android kotlin broadcast technology point
[template] longest common subsequence ([DP or greedy] board)
Codeforces Round #803 (Div. 2)(A~D)
Will your sleep service dream of the extra bookinfo on the service network
Astro learning notes
故事點 vs. 人天
Quantum three body problem: Landau fall
Whole house Wi Fi: a pain point that no one can solve?
Getting started with QT - making a simple calculator
P1042 [noip2003 popularization group] Table Tennis
BeanUtils -- shallow copy -- example / principle
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Achievements in science and Technology (27)
Solution: Compression Technology (original version and sequel version)
题解:《压缩技术》(原版、续集版)
SystemServer进程
Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
验证失败,请检查您的回电网址。您可以按照指导进行操作
qt中uic的使用