golang-gin-mysql转gorm-struct--gen-model

背景:python-django项目用go-gin重构,数据库已存在,gin中使用gorm 所以需要工具将mysql数据库中的表结构转到orm的model中。

前提:因为国内访问github需要稳定的代理

Goproxy.cn  推荐这个

golang-gin-mysql转gorm-struct--gen-model_第1张图片

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

  1. run init command, you will see .gen-model.yaml
cd ${your_project_dir}
gen-model init
  1. change mysql.* gen.searchTableName value in .gen-model.yaml, then run

golang-gin-mysql转gorm-struct--gen-model_第2张图片

 

gen-model create
  1. 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

golang-gin-mysql转gorm-struct--gen-model_第3张图片

  1. 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`

  1. persist mapper data in database just use db-mapper instead of local-mapper.
gen-model create --persist=db-mapper --forceCover=true
# it means, you can manage struct for multiple project.
# this require mysql `Create` privilege
  1. see what gen-model can do.
gen-model -h
gen-model create -h # commend `create` help

 

你可能感兴趣的:(mysql,golang,gin)