db.go 889 B

123456789101112131415161718192021222324252627282930313233343536
  1. package global
  2. import (
  3. "fmt"
  4. "gorm.io/driver/mysql"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/logger"
  7. "lot_interlligentControl/models"
  8. )
  9. var (
  10. DBLink *gorm.DB
  11. )
  12. func SetupDBLink() error {
  13. var err error
  14. dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s",
  15. DatabaseSetting.UserName,
  16. DatabaseSetting.Password,
  17. DatabaseSetting.Host,
  18. DatabaseSetting.Db,
  19. DatabaseSetting.OtherParams,
  20. )
  21. DBLink, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
  22. DisableForeignKeyConstraintWhenMigrating: true,
  23. Logger: logger.Default.LogMode(logger.Info),
  24. })
  25. if err != nil {
  26. return err
  27. }
  28. //DBLink.SingularTable(true)
  29. //DBLink.DB().SetMaxIdleConns(DatabaseSetting.MaxIdleConn)
  30. //DBLink.DB().SetMaxOpenConns(DatabaseSetting.MaxOpenConn)
  31. DBLink.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&models.ServiceNodes{}, &models.Shop{}, &models.Product{})
  32. return nil
  33. }