背景: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

  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

 

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

  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

 

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐