日常开发中,我们经常会有生成 数据库 model文件的需求,一个一个的码太慢。
就来试试 sql2struct工具吧
安装
go install github.com/starfishs/sql2struct@latest
生成 model
sql2struct -dsn="mysql://root:123456@tcp(localhost:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
大功告成。
本地安装 docker 的同学还可以直接使用 docker 运行
docker run -v /path/to/store:/workspace -w /workspace starfishsfive/sql2struct:v1.0.0 --dsn="postgresql://root:123456@localhost:5432/test?sslmode=disable"
ps: /path/to/store 替换为本地的路径
其他可选参数
sql2struct -h
sql2struct is a tool for generating golang struct from mysql/postgresql database.
Usage:
sql2struct [flags]
Flags:
-g, --dbtag string db tag. default: gorm (default "gorm")
--dsn string database dsn string (eg: root:123456@tcp(localhost:3306)/test?charset=utf8)
-h, --help help for sql2struct
-o, --output_dir string output dir. default: ./model (default "./model")
-p, --prefix string table prefixed with the table name
-t, --tables stringArray Need to generate tables, default is all tables. (eg: -t table1 -t table2).support regex
-j, --with_json_tag with json tag. default: true (default true)
使用过程中有任何问题,欢迎留言