main
lizifeng 2023-04-21 18:18:58 +08:00
parent 4c6d505685
commit aa0fb37da2
2 changed files with 11 additions and 11 deletions

View File

@ -53,11 +53,11 @@ func Instance(key ...string) *gorm.DB {
return instance return instance
} }
func NewDefault(config *Config) (*gorm.DB, error) { func InitDefault(config *Config) error {
return New(DefaultKey, config) return Init(DefaultKey, config)
} }
func New(key string, config *Config) (*gorm.DB, error) { func Init(key string, config *Config) error {
var ( var (
maxLifeTime, _ = time.ParseDuration(DefaultConfig.MaxLifeTime) maxLifeTime, _ = time.ParseDuration(DefaultConfig.MaxLifeTime)
maxIdleTime, _ = time.ParseDuration(DefaultConfig.MaxIdleTime) maxIdleTime, _ = time.ParseDuration(DefaultConfig.MaxIdleTime)
@ -75,7 +75,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
if config.MaxLifeTime != "" { if config.MaxLifeTime != "" {
t, err := time.ParseDuration(config.MaxLifeTime) t, err := time.ParseDuration(config.MaxLifeTime)
if err != nil { if err != nil {
return nil, fmt.Errorf("parse MaxLifeTime err: %s\n", err) return fmt.Errorf("parse MaxLifeTime err: %s\n", err)
} }
maxLifeTime = t maxLifeTime = t
@ -84,7 +84,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
if config.MaxIdleTime != "" { if config.MaxIdleTime != "" {
t, err := time.ParseDuration(config.MaxIdleTime) t, err := time.ParseDuration(config.MaxIdleTime)
if err != nil { if err != nil {
return nil, fmt.Errorf("parse MaxIdleTime err: %s\n", err) return fmt.Errorf("parse MaxIdleTime err: %s\n", err)
} }
maxIdleTime = t maxIdleTime = t
} }
@ -103,7 +103,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("connect mysql err: %s", err) return fmt.Errorf("connect mysql err: %s", err)
} }
sqlDb, _ := db.DB() sqlDb, _ := db.DB()
@ -113,7 +113,7 @@ func New(key string, config *Config) (*gorm.DB, error) {
sqlDb.SetConnMaxIdleTime(maxIdleTime) sqlDb.SetConnMaxIdleTime(maxIdleTime)
instanceMap[key] = db instanceMap[key] = db
return db, nil return nil
} }
func DefaultGormLogger(level gormLogger.LogLevel) gormLogger.Interface { func DefaultGormLogger(level gormLogger.LogLevel) gormLogger.Interface {

View File

@ -6,8 +6,8 @@ import (
) )
func TestMysql(t *testing.T) { func TestMysql(t *testing.T) {
myDB, err := NewDefault(&Config{ err := InitDefault(&Config{
Dsn: "root:root@tcp(127.0.0.1:3306)/site?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=2s&parseTime=true", Dsn: "root:Tqa129126@tcp(119.29.187.200:3306)/site?loc=Local&charset=utf8mb4&writeTimeout=3s&readTimeout=3s&timeout=2s&parseTime=true",
MaxOpenConn: 16, MaxOpenConn: 16,
MaxIdleConn: 4, MaxIdleConn: 4,
MaxIdleTime: "5m", MaxIdleTime: "5m",
@ -20,10 +20,10 @@ func TestMysql(t *testing.T) {
return return
} }
defer myDB.Close() defer CloseAll()
var res = make(map[string]interface{}) var res = make(map[string]interface{})
err = myDB.db.Table("image").Limit(1).Take(&res).Error err = Instance().Table("image").Limit(1).Take(&res).Error
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return