38 lines
985 B
Go
38 lines
985 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gen"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
// Dynamic SQL
|
||
|
type Querier interface {
|
||
|
// SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
|
||
|
FilterWithNameAndRole(name, role string) ([]gen.T, error)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
g := gen.NewGenerator(gen.Config{
|
||
|
OutPath: "../dbquery",
|
||
|
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
||
|
})
|
||
|
|
||
|
dsn := "root:joylink0503@tcp(192.168.3.233:3306)/bj-rtss?charset=utf8mb4&parseTime=true&loc=Local"
|
||
|
gormdb, err := gorm.Open(mysql.Open(dsn))
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
g.UseDB(gormdb) // reuse your gorm db
|
||
|
|
||
|
// Generate basic type-safe DAO API for struct `model.User` following conventions
|
||
|
g.ApplyBasic(g.GenerateAllTable()...)
|
||
|
|
||
|
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
|
||
|
// g.ApplyInterface(func(Querier) {}, entity.User{})
|
||
|
|
||
|
// Generate the code
|
||
|
g.Execute()
|
||
|
}
|