• Stars
    star
    2,307
  • Rank 19,150 (Top 0.4 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

database to golang struct

Build Status Go Report Card GoDoc Mentioned in Awesome Go

mysql database to golang struct conversion tools base on gorm(v1/v2),You can automatically generate golang sturct from mysql database. big Camel-Case Name Rule, JSON tag.

gui support

show

./gormt -g=true

cmd support

show

./gormt -g=false

install

go get -u -v github.com/xxjwxc/gormt@latest

or: Dowloading


1. Configure default configuration items through the current directory config.yml file

note: for latest version of config format, please check /data/config/MyIni.go

out_dir : "./model"  # out dir
url_tag : json # web url tag(json,db(https://github.com/google/go-querystring))
language :  # language(English,中 文)
db_tag : gorm # DB tag(gorm,db)
simple : false #simple output
is_out_sql : false # Whether to output sql
is_out_func : true # Whether to output function
is_foreign_key : true # Whether to mark foreign key or not
is_gui : false # Whether to operate on gui
is_table_name : false # Whether to out GetTableName/column function
is_null_to_point : false # database is 'DEFAULT NULL' then set element type as point
is_web_tag: false
is_web_tag_pk_hidden: false
table_prefix: "" #table prefix
table_names: "" # Specified table generation, multiple tables with , separated
is_column_name: true # Whether to generate column names
is_out_file_by_table_name: false # Whether to generate multiple models based on table names
db_info :
    host : "127.0.0.1"
    port : 3306
    username : "root"
    password : "qwer"
    database : "oauth_db"
    type: 0 # database type (0:mysql , 1:sqlite , 2:mssql)
self_type_define: # Custom data type mapping
    datetime: time.Time
    date: time.Time
out_file_name: "" # Custom build file name
web_tag_type: 0 # json tag 0: Small Camel-Case 1: _

2. get help

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

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

Usage:
  main [flags]

Flags:
  -d, --database string   数据库名
  -f, --foreign           是否导出外键关联
  -F, --fun               是否导出函数
  -g, --gui               是否ui显示模式
  -h, --help              help for main
  -H, --host string       数据库地址.(注意-H为大写)
  -o, --outdir string     输出目录
  -p, --password string   密码.
      --port int          端口号 (default 3306)
  -s, --singular          是否禁用表名复数
  -b, --table_names string 表名称  
  -l, --url string        url标签(json,url)
  -u, --user string       用户名.
  

3. Can be updated configuration items using command line tools

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

4. Support for gorm attributes

  • Database tables, column field annotation support
  • json tag json tag output
  • gorm.Model Support export gorm.model>>>
  • PRIMARY_KEY Specifies column as primary key
  • UNIQUE Specifies column as unique
  • NOT NULL Specifies column as NOT NULL
  • INDEX Create index with or without name, same name creates composite indexes
  • UNIQUE_INDEX Like INDEX, create unique index
  • Support foreign key related properties Support export gorm.model>>>
  • Support function export (foreign key, association, index , unique and more)Support export function >>>
  • model.Condition{} sql link

You can enrich data types in def

5. Demonstration

  • sql:
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_tbl_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`) USING BTREE,
  KEY `user_info_id` (`user_info_tbl_id`) USING BTREE,
  CONSTRAINT `user_account_tbl_ibfk_1` FOREIGN KEY (`user_info_tbl_id`) REFERENCES `user_info_tbl` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户账号'
--->Derived results
// UserAccountTbl 用户账号
type UserAccountTbl struct {
	ID            int    `gorm:"primary_key"`
	Account       string `gorm:"unique"`
	Password      string
	AccountType   int         // 帐号类型:0手机号,1邮件
	AppKey        string      // authbucket_oauth2_client表的id
	UserInfoTblID int         `gorm:"index"`
	UserInfoTbl   UserInfoTbl `gorm:"association_foreignkey:user_info_tbl_id;foreignkey:id"` // 用户信息
	RegTime       time.Time
	RegIP         string
	BundleID      string
	Describ       string
}

6. support func export

The exported function is only the auxiliary class function of Gorm, and calls Gorm completely

// FetchByPrimaryKey primary or index 获取唯一内容
func (obj *_UserAccountTblMgr) FetchByPrimaryKey(ID int) (result UserAccountTbl, err error) {
	err = obj.DB.Table(obj.GetTableName()).Where("id = ?", ID).Find(&result).Error
	if err == nil && obj.isRelated {
		{
			var info UserInfoTbl // 用户信息
			err = obj.DB.Table("user_info_tbl").Where("id = ?", result.UserInfoTblID).Find(&info).Error
			if err != nil {
				return
			}
			result.UserInfoTbl = info
		}
	}

	return
}

7. page

8. build

make windows
make linux
make mac

or

go generate

note : in windows not support utf-8 style . ASCALL model

  • Switch encoding mode
CHCP 65001 

column notes default

  • Add a comment to the column starting with [@gorm default:'test']
  • example [@gorm default:'test';->;<-:create]this is my notes Indicates that the default value is 'test',can read/creat/write
  • Use of foreign key notes[@fk tableName.columnName]this is my notes Represents the 'columnName' column associated with the 'tableName'

9. one windows gui tools

1

2

3

4

Download

Stargazers over time

Stargazers over time

More Repositories

1

uber_go_guide_cn

Uber Go 语言编码规范中文版. The Uber Go Style Guide .
7,286
star
2

caoguo

golang,微信小程序,电商系统
Vue
598
star
3

gowp

golang worker pool , Concurrency limiting goroutine pool
Go
488
star
4

shares

A-share quantitative system. A股量化系统
PLpgSQL
345
star
5

ginrpc

gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具
Go
287
star
6

public

util toolkit for go.golang 通用函数包
Go
165
star
7

gohanlp

Golang RESTful Client for HanLP.中文分词 词性标注 命名实体识别 依存句法分析 语义依存分析 新词发现 关键词短语提取 自动摘要 文本分类聚类 拼音简繁转换 自然语言处理
Go
35
star
8

gofal

fractional api base on golang . golang math tools fractional molecular denominator 分数计算 分子 分母 运算
Go
18
star
9

esLog

elasticsearch log golang 的elasticsearch 日志封装,包括搜索,查询,添加等
Go
18
star
10

xxjServer

C++ general tools,Cross-platform Tool Library
C
12
star
11

openai

openai interface on golang
Go
11
star
12

PoetryRhyme

Poetry and rhyme,诗古,诗,诗歌
10
star
13

go-service

golang service,golang 服务创建库,跨平台支持,golang 系统服务注册库
Go
10
star
14

RTMP

rtmp online, 直播
C
10
star
15

consult

golang consul tools
Go
7
star
16

xxjwxc.github.io

个人博客地址,欢迎访问
HTML
6
star
17

oauth2

golang oauth2 authorization tools , oauth2通用授权系统
C
6
star
18

SQLserverCppConnection

SQLserver Connection for cpp ,c++ sqlserver 链接库
C++
5
star
19

jump

remote monitoring . 远程监控工具
Go
5
star
20

ginrest

restful api base on go gin
Go
4
star
21

GoSparkApi

golang 讯飞星火大模型 Go SparkApi
Go
4
star
22

douyin

golang 抖音弹幕消息
HTML
3
star
23

gomp3

mp3 decode mp3编解码器 golang版本 (pcm wav mp3)
C
2
star
24

xxjwxc

1
star
25

gocharts

golang echarts tools
Go
1
star