GO 如何像mybatisGenerator一样通过数据库表生成对应的实体呢?
背景 :学习gorm的过程中,struct的tag注解偏多,实体模型多的情况下,一个个写tag太慢。
思考 :寻思着有没有类似mybatisGenerator呢样的逆向工程,生成相关的bean模型,并通过tag做好orm映射。
行动 : github 进行搜索相关项目
1. 一款叫做 fuckDB 的开源项目可以通过Web页面,进行点点,进行配置。但是无论尝试直接go get ,还是基于docker都无法正确使用,前者是因为拉不下来指定的golang.org/x/sys ,后者安装正确但是交互无法应,定位不出需要修改的文件位置。
2. 在经过一段的时间的搜索后,发现了可以简单使用的生成工具 db2struct。经过readme '照本宣科’的指引后,达到了想要的结果,再此记录下操作步骤。
操作步骤
- go get github.com/Shelnutt2/db2struct/cmd/db2struct
- 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 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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.
修改了相应环境变量后解决。