话不多说直接上代码 测试用表:
单表生成
简单生成一个表对应的模型文件
package main
import (
"github.com/pulingfu/tblschema"
)
func main() {
simple := tblschema.NewTblToStructHandler()
simple.
SetDsn("root:tiger@(127.0.0.1:3306)/pulingfu?charset=utf8mb4&parseTime=True&loc=Local").
SetTableName("my_user1").
SetSavePath("./simple/model.go").
GenerateTblStruct()
}
直接运行go run main.go就生成如下表对应的模型文件
============我是分界线+++++++++++++++++++++++++
还可以设置包名、生成路径、标签、生成结构体名、成员名等如下:
package main
import (
"github.com/pulingfu/tblschema"
)
func main() {
th := tblschema.NewTblToStructHandler()
th = th.
SetDsn("root:tiger@(127.0.0.1:3306)/pulingfu?charset=utf8mb4&parseTime=True&loc=Local").
SetPackageInfo("tbl_my_user", "prefix_", "_suffix").
SetTableName("my_user").
SetTblStructNameInfo(tblschema.CAMEL_CASE, "a我是_table_name前缀_", "_b我是_table_name后缀").
SeTblStructColumnNameInfo(tblschema.CAMEL_CASE, "", "a我是_column_name前缀_", "_b我是_column_name后缀").
SetStructOrmTag("gorm").
SetSavePath("./tbl_my_user/model.go").
GenerateTblStruct()
th.
SetPackageInfo("tbl_my_user_timestring", "prefix_", "_suffix").
SetOtherTags("").
SetSavePath("./tbl_my_user_time_string/model.go").
SetTimeType(tblschema.TIMETYPE_STRING).
SetTblStructNameInfo(tblschema.CAMEL_CASE, "", "").
SeTblStructColumnNameInfo(tblschema.CAMEL_CASE, tblschema.FIELD_ORDER_FIELD_NAME, "", "").
GenerateTblStruct()
}
go run main.go就会生成如下文件
============我是分界线+++++++++++++++++++++++++
批量生成多表
直接上代码
package main
import "github.com/pulingfu/tblschema"
func main() {
th := tblschema.NewTblToStructHandler()
th.SetDsn("root:tiger@(127.0.0.1:3306)/pulingfu?charset=utf8mb4&parseTime=True&loc=Local").
GenerateAllTblStruct()
}
go run main.go就会生成如下容
============我是分界线+++++++++++++++++++++++++
自定义批量生成内容
package main
import (
"fmt"
"github.com/pulingfu/tblschema"
)
func main() {
th := tblschema.NewTblToStructHandler().
SetDsn("root:tiger@(127.0.0.1:3306)/pulingfu?charset=utf8mb4&parseTime=True&loc=Local").
SetOtherTags("json").
SetPackageInfo("all_tbl_model", "", "")
for _, tname := range th.GetAllTableNames() {
th.
SetSavePath(fmt.Sprintf("./all_tbl_model/%s.go", tname)).
SetTableName(tname).
GenerateTblStruct()
}
}
执行命令 go run main.go 就会生成如下内容
|