0

golang自动生成对应MySQL数据库表的struct定义

 2 years ago
source link: https://cloudsjhan.github.io/2020/01/01/golang%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%AF%B9%E5%BA%94MySQL%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8%E7%9A%84struct%E5%AE%9A%E4%B9%89/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

golang自动生成对应MySQL数据库表的struct定义

发表于 2020-01-01

| 分类于 golang

| 阅读次数:

| 字数统计: 811

|

阅读时长 ≈ 3

golang自动生成对应MySQL数据库表的struct定义

在golang的开发过程中,当我们使用orm的时候,常常需要将数据库表对应到golang的一个struct,这些struct会携带orm对应的tag,就像下面的struct定义一样:

type InsInfo struct {
Connections string `gorm:"column:connections"`
CPU int `gorm:"column:cpu"`
CreateTime time.Time `gorm:"column:create_time"`
Env int `gorm:"column:env"`
ID int64 `gorm:"column:id;primary_key"`
IP string `gorm:"column:ip"`
Organization string `gorm:"column:organization"`
Pass string `gorm:"column:pass"`
Port string `gorm:"column:port"`
RegionId string `gorm:"column:regionid"`
ServerIP string `gorm:"column:server_ip"`
Status int `gorm:"column:status"`
Type string `gorm:"column:type"`
UUID string `gorm:"column:uuid"`
}

这是gorm对应的数据库表的struct映射,即使数据表的字段不多,如果是手动写起来也是一些重复性的工作。像MySQL这种关系型数据库,我们一般会用orm去操作数据库,于是就想,mysql的数据表能不能来自动生成golang 的struct定义。我们知道mysql有个自带的数据库information_schema,有一张表COLUMNS,它的字段包含数据库名、表名、字段名、字段类型等,我们可以利用这个表的数据,把对应的表的字段信息读取出来,然后再根据golang的语法规则,生成对应的struct。
调研了一下目前有一些命令行工具像 db2struct等,感觉用起来比较繁琐,在想能不能提供一个开箱即用的环境,提供web界面,我们只需要填写数据库信息,就可以一键生成对应的ORM的struct,于是就诞生了这个项目:https://github.com/hantmac/fuckdb

如果你的数据库在本地,那么只需要执行 docker-compose up -d,访问localhost:8088,你就会得到下面的界面:

如果你的数据库在内网服务器上,你需要先修改后端接口的ip:port,然后重新build Docker镜像,push到自己的镜像仓库,然后修改docker-compose.yaml,再执行docker-compose up -d。修改的位置是:fuckdb/frontend/src/config/index.js.

只需要填入数据库相关信息,以及你想得到的golang代码的package namestruct name,然后点击生成,就可以得到gorm对应的结构体映射:

你只需要拷贝你的代码到项目中即可。我们都知道golang的struct的tag有很多功能,这里也提供了很多tag的可选项,比如json,xml等,后面会曾加更多的tag可选项支持。

上面的GIF展示了增加了缓存功能的版本,记忆你之前填写过的数据库信息,省去了大量重复的操作,你不用再填写繁琐的数据库名,表名,等等,只需一键,就可以得到对应的代码,是不是很方便啊。ps:目前数据库信息没有做加密,所以不方便放到公网上使用,仅限于内网,后面会进行相应的开发支持。目前这个工具在我们组内已经开始使用,反馈比较好,节省了很多重复的工作,尤其是在开发的时候用到同一个库的多张表,很快就可以完成数据库表->strcut的映射。

欢迎试用&反馈&Contribute。代码地址:https://github.com/hantmac/fuckdb


-------------The End-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
(>给这篇博客打个分吧<)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK