手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2018年11月5日的文章

将表结构转成gorm struct

 在网上找了半天,找到两个项目与此相关

1、chrome的插件:https://github.com/mikemintang/sql2struct ,
2、go 程序:https://github.com/Shelnutt2/db2struct
 
1、chrome插件的Readme中所引用的crx文件,可能由于cdn的问题,无法下载,其实git里就有一个crx文件,点击查看详情,然后下载到本地。启用chrome extension的开发者模式,加载进来就OK了,使用方法很简单,直接将table 的ddl内容复制 到输入框里,就能够生成相应的struct。当然表前缀的问题是没法解决了,但至少你不用手写了。不是吗?
详细 教程看这个:http://idoubi.cc/2017/11/06/SQL2Struct%EF%BC%9A%E4%B8%80%E6%AC%BE%E6%A0%B9%E6%8D%AEsql%E8%AF%AD%E5%8F%A5%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90golang%E7%BB%93%E6%9E%84%E4%BD%93%E7%9A%84chrome%E6%8F%92%E4%BB%B6/,主要是这个有图,github里面没图
 
2、用官方Readme中的说法:Structures are created by querying the INFORMATION_SCHEMA.Columns table and then formatting the types, column names, and metadata to create a usable go compatible struct type.
用法其实也很简单:【Example below uses guregu's null package, but without the option it procuded the sql.NullInt64 and so on.
XML/HTML代码
  1. db2struct --host localhost -d example.com -t users --package example --struct user -p --user exampleUser --guregu --gorm  
 
有这两个工具,生成struct就方便了。如果再配合这个:https://github.com/MohamedBassem/gormgen,那就更方便了(虽然也不一定会这样使用)
 

Tags: gorm