fafa-crawler/config/gen_mapper.go
2025-11-21 23:21:20 +08:00

63 lines
1.4 KiB
Go
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"strings"
_ "github.com/go-sql-driver/mysql"
"gorm.io/driver/mysql"
"gorm.io/gen"
"gorm.io/gorm"
)
func main() {
dsn := "root:1234567890@tcp(localhost:3306)/goods_library?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("gorm 打开数据库出错:" + err.Error())
}
g := gen.NewGenerator(gen.Config{
OutPath: "./src/mapper",
ModelPkgPath: "./src/models",
Mode: gen.WithDefaultQuery | gen.WithQueryInterface | gen.WithoutContext,
FieldNullable: false,
FieldCoverable: false,
FieldSignable: true,
FieldWithIndexTag: true,
FieldWithTypeTag: true,
})
g.UseDB(db)
tbNames, err := db.Migrator().GetTables()
if err != nil {
panic("get all tables fail: " + err.Error())
}
metas := make([]interface{}, 0)
for _, v := range tbNames {
if !strings.Contains(v, "_20") && !strings.Contains(v, "_default") {
metas = append(metas, g.GenerateModel(v))
}
}
g.ApplyBasic(metas...)
// g.ApplyBasic(g.GenerateAllTable()...)
// g.ApplyBasic(
// g.GenerateModel("account_balance_log"),
// )
g.Execute()
// 批量替换文件夹的内容
// modelPath, err := filepath.Abs("./src/models")
// log.Println("models path is" + modelPath)
// if err != nil {
// log.Println(err)
// }
// log.Println("models file have replace")
}