You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
753 B
40 lines
753 B
package svc
|
|
|
|
import (
|
|
{{.configImport}}
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
// xxx "{项目名称}/model/xxx" //原版注册方法
|
|
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config {{.config}}
|
|
{{.middleware}}
|
|
GormDb *gorm.DB
|
|
// XxxModel xxx.xxxModel //原版注册方法
|
|
}
|
|
|
|
func NewServiceContext(c {{.config}}) *ServiceContext {
|
|
// 初始化Gorm ORM
|
|
dsn := c.MysqlConnStr
|
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// // 原版初始化 XxxModel
|
|
//conn := sqlx.NewMysql(c.MysqlConnStr)
|
|
//if err != nil {
|
|
// panic(err)
|
|
//}
|
|
|
|
|
|
return &ServiceContext{
|
|
Config: c,
|
|
GormDb: db,
|
|
{{.middlewareAssignment}}
|
|
//XxxModel: xxx.NewXxxModel(conn, c.CacheRedis), //原版注册方法 带缓存
|
|
|
|
}
|
|
}
|
|
|