We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
比如增加一个order数据库,repository.go增加: func NewOrderDB(conf *viper.Viper) *gorm.DB { dbOrder, err := gorm.Open(mysql.Open(conf.GetString("data.mysql.order")), &gorm.Config{}) if err != nil { panic(fmt.Sprintf("mysql error: %s", err.Error())) } return dbOrder }
func NewOrderDB(conf *viper.Viper) *gorm.DB { dbOrder, err := gorm.Open(mysql.Open(conf.GetString("data.mysql.order")), &gorm.Config{}) if err != nil { panic(fmt.Sprintf("mysql error: %s", err.Error())) } return dbOrder }
wire.go增加如下: 然后执行nunu wire all 报错:
The text was updated successfully, but these errors were encountered:
报错是由于wire不支持多个同类型参数注入,解决该报错也很简单,我们自己定义对应的新类型来使用
type OrderDB gorm.DB type UserDB gorm.DB
完整示例代码如下
internal/repository/repository.go
type UserDB gorm.DB type OrderDB gorm.DB type Repository struct { userDB *gorm.DB orderDB *gorm.DB rdb *redis.Client logger *log.Logger } func NewRepository(userDB *UserDB, orderDB *OrderDB, rdb *redis.Client, logger *log.Logger) *Repository { return &Repository{ userDB: (*gorm.DB)(userDB), orderDB: (*gorm.DB)(orderDB), rdb: rdb, logger: logger, } } func NewUserDB(conf *viper.Viper, l *log.Logger) *UserDB { logger := zapgorm2.New(l.Logger) logger.SetAsDefault() db, err := gorm.Open(mysql.Open(conf.GetString("data.mysql.user")), &gorm.Config{Logger: logger}) if err != nil { panic(err) } db = db.Debug() return (*UserDB)(db) } func NewOrderDB(conf *viper.Viper, l *log.Logger) *OrderDB { logger := zapgorm2.New(l.Logger) logger.SetAsDefault() db, err := gorm.Open(mysql.Open(conf.GetString("data.mysql.order")), &gorm.Config{Logger: logger}) if err != nil { panic(err) } db = db.Debug() return (*OrderDB)(db) } // ...
internal/repository/user.go
func (r *userRepository) Create(ctx context.Context, user *model.User) error { if err := r.userDB.Create(user).Error; err != nil { return err } return nil }
Sorry, something went wrong.
多谢大牛
No branches or pull requests
比如增加一个order数据库,repository.go增加:
func NewOrderDB(conf *viper.Viper) *gorm.DB { dbOrder, err := gorm.Open(mysql.Open(conf.GetString("data.mysql.order")), &gorm.Config{}) if err != nil { panic(fmt.Sprintf("mysql error: %s", err.Error())) } return dbOrder }
wire.go增加如下:
然后执行nunu wire all 报错:
The text was updated successfully, but these errors were encountered: