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.

31 lines
901 B

  1. package initialize
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/initialize/internal"
  5. "gorm.io/driver/mysql"
  6. "gorm.io/gorm"
  7. )
  8. // GormMysql 初始化Mysql数据库
  9. // Author [piexlmax](https://github.com/piexlmax)
  10. // Author [SliverHorn](https://github.com/SliverHorn)
  11. func GormMysql() *gorm.DB {
  12. m := global.GVA_CONFIG.Mysql
  13. if m.Dbname == "" {
  14. return nil
  15. }
  16. mysqlConfig := mysql.Config{
  17. DSN: m.Dsn(), // DSN data source name
  18. DefaultStringSize: 191, // string 类型字段的默认长度
  19. SkipInitializeWithVersion: false, // 根据版本自动配置
  20. }
  21. if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil {
  22. return nil
  23. } else {
  24. sqlDB, _ := db.DB()
  25. sqlDB.SetMaxIdleConns(m.MaxIdleConns)
  26. sqlDB.SetMaxOpenConns(m.MaxOpenConns)
  27. return db
  28. }
  29. }