通过反转数据库表信息自动生成ORM代码
go get github.com/youngchan1988/ormgenc
Clone : [email protected]:youngchan1988/ormgenc.git
执行 :
$ cd ormgenc
$ go install
$ ormgenc --help
Usage of ormgenc:
-config string
set config file path (default "./config.yaml")
-db string
set target database which like 'postgresql', 'mysql', 'sqlite' and so on (default "postgresql")
-debug
sets log level to debug
-help
ormgenc usage
-orm string
set target orm which like 'gorm' and 'xorm' (default "gorm")
-out string
set output path (default ".")
-version
show current version
配置文件中包含连接数据库的信息:
db_host: "127.0.0.1"
db_name: "apptoygodb"
db_user: "newcore"
db_password: "xinheyun2014@0711"
db_port: 5433
生成的ORM代码中包含两个方法:EntityToModel
和 ModelToEntity
,方便在业务Entity和数据库Model 相互转换。
比如:
//Define User struct
type UserEntity struct {
ID int `json:"id" ormgen:"Id"`
Name string `json:"name" ormgen:"Name"`
MobilePhone string `json:"mobilePhone" ormgen:"MobilePhone"`
Email string `json:"email" ormgen:"Email"`
State int `json:"state" ormgen:"State"`
Info *UserInfoEntity `json:"info" ormgen:"Info"`
CreatedDate time.Time `json:"createdDate" ormgen:"CreatedDate"`
UpdatedDate time.Time `json:"updatedDate" ormgen:"UpdatedDate"`
DeletedDate time.Time `json:"deleteDate" ormgen:"DeletedDate"`
}
//UserInfoEntity
type UserInfoEntity struct {
Avatar string `json:"avatar"`
}
type UserDBModel struct {
Id int `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
Password string `gorm:"column:password"`
MobilePhone string `gorm:"column:mobile_phone"`
Email string `gorm:"column:email"`
State int `gorm:"column:state"`
Info *pgtype.JSONB `gorm:"column:info"`
CreatedDate time.Time `gorm:"column:created_date;autoCreateTime"`
UpdatedDate time.Time `gorm:"column:updated_date;autoUpdateTime"`
DeletedDate gorm.DeletedAt `gorm:"column:deleted_date;index"`
}
在Entity中使用ormgen
struct tag 来标识这个字段在数据库Model中对应的名称,针对Postgresql
数据支持jsonb
数据类型的转换
-
gorm
-
xorm
-
postgresql
-
mysql