package global import ( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "lot_interlligentControl/models" ) var ( DBLink *gorm.DB ) func SetupDBLink() error { var err error dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s", DatabaseSetting.UserName, DatabaseSetting.Password, DatabaseSetting.Host, DatabaseSetting.Db, DatabaseSetting.OtherParams, ) DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ DisableForeignKeyConstraintWhenMigrating: true, Logger: logger.Default.LogMode(logger.Info), }) if err != nil { return err } //DBLink.SingularTable(true) //DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConn) //DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConn) DBLink.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&models.ServiceNodes{}, &models.Shop{}, &models.Product{}) return nil }