go 将数据库转换为 gorm 结构

2019-11-29 10:17:55 +08:00
 xxjwxc

gormt

gorm mysql 数据库转 struct 工具,可以将 mysql 数据库自动生成 golang sturct 结构,带大驼峰命名规则。带 json 标签


1. 通过当前目录 config.toml 文件配置默认配置项

out_dir = "."  # 输出目录
singular_table = false  # 表名复数,是否大驼峰构建 参考:gorm.SingularTable
simple = false #简单输出
isJsonTag = true #是否打 json 标记
[mysql_info]
    host = "127.0.0.1"
    port = 3306
    username = "root"
    password = "qwer"
    database = "oauth_db"

2. 可以使用命令行工具更新配置项

./gormt -H=127.0.0.1 -d=oauth_db -p=qwer -u=root --port=3306

3. 查看帮助

./gormt --help
or
./gormt -h

-------------------------------------------------------
base on gorm tools for mysql database to golang struct

Usage:
  main [flags]

Flags:
  -d, --database string   数据库名
  -h, --help              help for main
  -H, --host string       数据库地址.(注意-H 为大写)
  -o, --outdir string     输出目录
  -p, --password string   密码.
      --port int          端口号 (default 3306)
  -s, --singular          是否禁用表名复数
  -u, --user string       用户名.
  

4. 支持 gorm 相关属性

5. 示例展示

CREATE TABLE `user_account_tbl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `account_type` int(11) NOT NULL DEFAULT '0' COMMENT '帐号类型:0 手机号,1 邮件',
  `app_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'authbucket_oauth2_client 表的 id',
  `user_info_id` int(11) NOT NULL,
  `reg_time` datetime DEFAULT NULL,
  `reg_ip` varchar(15) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `bundle_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `describ` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `account` (`account`),
  UNIQUE KEY `UNIQ_5696AD037D3656A4` (`app_key`,`user_info_id`) USING BTREE,
  KEY `user_info_id` (`user_info_id`),
  CONSTRAINT `1` FOREIGN KEY (`user_info_id`) REFERENCES `user_info_tbl` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='用户信息'
--->导出结果
//	用户信息
type UserAccountTbl struct {
	ID          int       `gorm:"primary_key;column:id;type:int(11);not null" json:"-"`                                                   //
	Account     string    `gorm:"unique;column:account;type:varchar(64);not null" json:"account"`                                         //
	Password    string    `gorm:"column:password;type:varchar(64);not null" json:"password"`                                              //
	AccountType int       `gorm:"column:account_type;type:int(11);not null" json:"account_type"`                                          //	帐号类型:0 手机号,1 邮件
	AppKey      string    `json:"app_key" gorm:"unique_index:UNIQ_5696AD037D3656A4;column:app_key;type:varchar(255);not null"`            //	authbucket_oauth2_client 表的 id
	UserInfoID  int       `gorm:"unique_index:UNIQ_5696AD037D3656A4;index;column:user_info_id;type:int(11);not null" json:"user_info_id"` //
	RegTime     time.Time `gorm:"column:reg_time;type:datetime" json:"reg_time"`                                                          //
	RegIP       string    `gorm:"column:reg_ip;type:varchar(15)" json:"reg_ip"`                                                           //
	BundleID    string    `json:"bundle_id" gorm:"column:bundle_id;type:varchar(255)"`                                                    //
	Describ     string    `gorm:"column:describ;type:varchar(255)" json:"describ"`                                                        //
}
--->导出结果
type User_account_tbl struct {
	Id           int       `gorm:"primary_key;column:id;type:int(11);not null" json:"-"`                                                   //
	Account      string    `gorm:"unique;column:account;type:varchar(64);not null" json:"account"`                                         //
	Password     string    `gorm:"column:password;type:varchar(64);not null" json:"password"`                                              //
	Account_type int       `gorm:"column:account_type;type:int(11);not null" json:"account_type"`                                          //	帐号类型:0 手机号,1 邮件
	App_key      string    `gorm:"unique_index:UNIQ_5696AD037D3656A4;column:app_key;type:varchar(255);not null" json:"app_key"`            //	authbucket_oauth2_client 表的 id
	User_info_id int       `gorm:"unique_index:UNIQ_5696AD037D3656A4;index;column:user_info_id;type:int(11);not null" json:"user_info_id"` //
	Reg_time     time.Time `gorm:"column:reg_time;type:datetime" json:"reg_time"`                                                          //
	Reg_ip       string    `gorm:"column:reg_ip;type:varchar(15)" json:"reg_ip"`                                                           //
	Bundle_id    string    `gorm:"column:bundle_id;type:varchar(255)" json:"bundle_id"`                                                    //
	Describ      string    `gorm:"column:describ;type:varchar(255)" json:"describ"`                                                        //
}
--->导出结果
//	用户信息
type UserAccountTbl struct {
	ID          int       `json:"-" gorm:"primary_key"`                                         //
	Account     string    `gorm:"unique" json:"account"`                                        //
	Password    string    `json:"password"`                                                     //
	AccountType int       `json:"account_type"`                                                 //	帐号类型:0 手机号,1 邮件
	AppKey      string    `gorm:"unique_index:UNIQ_5696AD037D3656A4" json:"app_key"`            //	authbucket_oauth2_client 表的 id
	UserInfoID  int       `json:"user_info_id" gorm:"unique_index:UNIQ_5696AD037D3656A4;index"` //
	RegTime     time.Time `json:"reg_time"`                                                     //
	RegIP       string    `json:"reg_ip"`                                                       //
	BundleID    string    `json:"bundle_id"`                                                    //
	Describ     string    `json:"describ"`                                                      //
}
--->导出结果
//	用户信息
type UserAccountTbl struct {
	ID          int       `gorm:"primary_key"` //
	Account     string    `gorm:"unique"`      //
	Password    string    //
	AccountType int       //	帐号类型:0 手机号,1 邮件
	AppKey      string    `gorm:"unique_index:UNIQ_5696AD037D3656A4"`       //	authbucket_oauth2_client 表的 id
	UserInfoID  int       `gorm:"unique_index:UNIQ_5696AD037D3656A4;index"` //
	RegTime     time.Time //
	RegIP       string    //
	BundleID    string    //
	Describ     string    //
}
CREATE TABLE `user_info_tbl` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nickname` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `headurl` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='用户信息'
--->导出结果
//	用户信息
type UserInfoTbl struct {
	gorm.Model        //
	Nickname   string //
	Headurl    string //
}

6. 提供一个 windows 可视化工具

下载地址

7. 下一步计划

4776 次点击
所在节点    Go 编程语言
12 条回复
qq316107934
2019-11-29 11:09:46 +08:00
一直在用,作者居然在 V 站,帮顶下。
用着蛮方便的,问题是支持的类型有点少,要手动改源代码加一些类型才能跑通。
hijoker
2019-11-29 11:19:05 +08:00
gorm 是没有自带这种工具的,xorm 好像自带命令行工具
xxjwxc
2019-11-29 12:56:59 +08:00
@qq316107934 哈哈,感谢支持。类型已经完善很多了。
xxjwxc
2019-11-29 12:57:28 +08:00
@hijoker 是的。所以才有了这个工具
zuokanyunqishi
2019-11-29 13:07:04 +08:00
大佬那个 Windows 的 gui 用什么写的啊
xxjwxc
2019-11-29 14:41:10 +08:00
dishonest
2019-11-29 15:56:18 +08:00
gorm 用得想吐血。
移植一个现有程序,数据库 model 用 rails 我 30 分钟就搞定的事情,go/gorm 需要手工做,可能限于个人能力,一周了都时常发现一些致命 bug,心力交瘁。go 在 web 这块应该要好好加强一下。
xxjwxc
2019-11-29 16:16:53 +08:00
@dishonest 建议使用 gorm 的常用接口。gormt 支持所有带驼峰规则的 orm。比如 xorm,fastdb
xrlin
2019-12-01 06:19:50 +08:00
在原型开发速度方面 go 的所有框架都比不上 rails
dishonest
2019-12-02 17:25:57 +08:00
@xrlin 去掉"go 的"
xrlin
2019-12-02 23:29:06 +08:00
@dishonest 哈哈,在 web 开发速度上,我愿称 rails 为最强,ruby 的表达能力也是一马当先。
ifsclimbing
2020-08-11 09:49:43 +08:00
@xrlin
@dishonest
rails 太香了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/624196

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX