package main
import (
"gorm.io/driver/mysql"
"gorm.io/gen"
"gorm.io/gorm"
)
const MySQLDSN = "root:password@tcp(ip:prod)/database_name?charset=utf8mb4&parseTime=True&loc=Local"
func main() {
g := gen.NewGenerator(gen.Config{
//OutPath: "./gen/query",
OutPath: "./gen/gen_query",
//ModelPkgPath: "./gen/model",
ModelPkgPath: "./gen_model",
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
})
gormdb, _ := gorm.Open(mysql.Open(MySQLDSN))
g.UseDB(gormdb) // reuse your gorm db
g.ApplyBasic(
// Generate struct `User` based on table `users`
g.GenerateModel("wx_profession"),
)
// Generate the code
g.Execute()
}
运行代码,生成下面代码
初始化赋值数据库
使用
评论区