背景:python-django项目用go-gin重构,数据库已存在,gin中使用gorm 所以需要工具将mysql数据库中的表结构转到orm的model中。
前提:因为国内访问github需要稳定的代理
Goproxy.cn 推荐这个
1.在项目路径中下载gen-model模块
go get -u github.com/DaoYoung/gen-model
# GOBIN 目录下会有一个gen-model.exe
# 如果安装失败--:https://github.com/DaoYoung/gen-model 下载源代码,进到gen-model, 手动 go install
Usage
- run
init
command, you will see.gen-model.yaml
cd ${your_project_dir}
gen-model init
- change
mysql.*
gen.searchTableName
value in.gen-model.yaml
, then run
gen-model create
- create local mappers for struct
gen-model create --persist=local-mapper
# it will fail, when run after step 2, because struct file is already exist, it's avoid to cover whole file. you can set `-f=true` to cover it.
gen-model create --persist=local-mapper -f=true
- rename mapper file from
${struct}FieldMapper.yaml
to${struct}VOFieldMapper.yaml
, and delete one line after fields
gen-model create --source=local-mapper --forceCover=true --modelSuffix=VO
# it will generate `${struct}VO.go`
- persist mapper data in database just use
db-mapper
instead oflocal-mapper
.
gen-model create --persist=db-mapper --forceCover=true
# it means, you can manage struct for multiple project.
# this require mysql `Create` privilege
- see what gen-model can do.
gen-model -h
gen-model create -h # commend `create` help