initMysql.go 607 B

12345678910111213141516171819202122
  1. package qmsql
  2. import (
  3. "gin-vue-admin/config"
  4. "gin-vue-admin/init/initlog"
  5. "github.com/jinzhu/gorm"
  6. _ "github.com/jinzhu/gorm/dialects/mysql"
  7. )
  8. var DEFAULTDB *gorm.DB
  9. //初始化数据库并产生数据库全局变量
  10. func InitMysql(admin config.MysqlAdmin, logger log.Logger) *gorm.DB {
  11. if db, err := gorm.Open("mysql", admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil {
  12. logger.Error("DEFAULTDB数据库启动异常",err)
  13. } else {
  14. DEFAULTDB = db
  15. DEFAULTDB.DB().SetMaxIdleConns(10)
  16. DEFAULTDB.DB().SetMaxOpenConns(100)
  17. }
  18. return DEFAULTDB
  19. }