Go语言orm逆向工程 基于mysql生成struct并添加sql/gorm tag、json tag

GO 如何像mybatisGenerator一样通过数据库表生成对应的实体呢?

背景:学习gorm的过程中,struct的tag注解偏多,实体模型多的情况下,一个个写tag太慢。

思考:寻思着有没有类似mybatisGenerator呢样的逆向工程,生成相关的bean模型,并通过tag做好orm映射。

行动: github 进行搜索相关项目

1. 一款叫做 fuckDB 的开源项目可以通过Web页面,进行点点,进行配置。但是无论尝试直接go get ,还是基于docker都无法正确使用,前者是因为拉不下来指定的golang.org/x/sys ,后者安装正确但是交互无法应,定位不出需要修改的文件位置。

2. 在经过一段的时间的搜索后,发现了可以简单使用的生成工具 db2struct。经过readme '照本宣科’的指引后,达到了想要的结果,再此记录下操作步骤。


操作步骤
  1. go get github.com/Shelnutt2/db2struct/cmd/db2struct
  2. db2struct --host localhost -d test -t test_table --package myGoPackage --struct testTable -p --user testUser

其中参数定义可以通过-h查看

Usage of db2struct:
db2struct [-H] [-p] [-v] --package pkgName --struct structName --database databaseName --table tableName
Options:
-H, --host= Host to check mariadb status of
–mysql_port=3306 Specify a port to connect to
-t, --table= Table to build struct from
-d, --database=nil Database to for connection
-u, --user=user user to connect to database
-v, --verbose Enable verbose output
–package= name to set for package
–struct= name to set for struct
–json Add json annotations (default)
–no-json Disable json annotations
–gorm Add gorm annotations (tags)
–guregu Add guregu null types
–target= Save file path
-p, --password= Mysql password
-h, --help Show usage message
–version Show version

产生出来的struct 文件

type AgentOrgOperatorManage struct {
	CREATEDAT       time.Time      `gorm:"column:CREATED_AT" json:"CREATED_AT"`
	CREATEDBY       string         `gorm:"column:CREATED_BY" json:"CREATED_BY"`
	UPDATEDAT       time.Time      `gorm:"column:UPDATED_AT" json:"UPDATED_AT"`
	UPDATEDBY       string         `gorm:"column:UPDATED_BY" json:"UPDATED_BY"`
	CityCode        sql.NullString `gorm:"column:city_code" json:"city_code"`
	CityName        sql.NullString `gorm:"column:city_name" json:"city_name"`
	ID              int64          `gorm:"column:id;primary_key" json:"id"`
	OperatorLoginNo string         `gorm:"column:operator_login_no" json:"operator_login_no"`
	OperatorName    sql.NullString `gorm:"column:operator_name" json:"operator_name"`
	OperatorPhone   sql.NullString `gorm:"column:operator_phone" json:"operator_phone"`
	OperatorType    sql.NullString `gorm:"column:operator_type" json:"operator_type"`
	OrgID           sql.NullString `gorm:"column:org_id" json:"org_id"`
	OrgName         sql.NullString `gorm:"column:org_name" json:"org_name"`
	OrgType         sql.NullString `gorm:"column:org_type" json:"org_type"`
	ProvinceCode    sql.NullString `gorm:"column:province_code" json:"province_code"`
	ProvinceName    sql.NullString `gorm:"column:province_name" json:"province_name"`
	Status          sql.NullString `gorm:"column:status" json:"status"`
}

// TableName sets the insert table name for this struct type
func (a *AgentOrgOperatorManage) TableName() string {
	return "t_agent_org_operator_manage"
}
//db2struct --host 127.0.0.1  -d o2o --package moudle  -p 123456 --user root -t t_delivery_store_operator_record --struct DeliveryStoreOperatorRecord --gorm --json

其中碰到的问题:db2struct: command not found

发现issures有人提到了同样的问题,再回想之前通过zsh连接mysql的时候好像也出现过这个题,most likely the $GOPATH/bin is not in your $PATH variable. Try running $GOPATH/bin/db2struct --help.

bash_profile
修改了相应环境变量后解决。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值