diff --git a/.gitignore b/.gitignore index 20d3308..b193604 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea vendor -logs \ No newline at end of file +logs +*.db \ No newline at end of file diff --git a/config/mysql.go b/config/mysql.go index f120f2d..2d7bec1 100644 --- a/config/mysql.go +++ b/config/mysql.go @@ -1,5 +1,7 @@ package config +import "fmt" + // MySQL配置 type mysqlConfig struct { Host string // 主机 @@ -25,3 +27,9 @@ func InitMySQLConfig() { DbName: dbName, } } + +// GetDSN 返回 MySQL 连接字符串 +func (c mysqlConfig) GetDSN() string { + return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", + c.Username, c.Password, c.Host, c.Port, c.DbName) +} diff --git a/global/mysql.go b/global/mysql.go index 428d643..2ff7d8f 100644 --- a/global/mysql.go +++ b/global/mysql.go @@ -1,7 +1,6 @@ package global import ( - "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" @@ -25,9 +24,7 @@ func InitMySQLClient() { }, ) - url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", - MySQLConfig.Username, MySQLConfig.Password, MySQLConfig.Host, MySQLConfig.Port, MySQLConfig.DbName) - conn, err := gorm.Open(mysql.Open(url), &gorm.Config{Logger: newLogger}) + conn, err := gorm.Open(mysql.Open(MySQLConfig.GetDSN()), &gorm.Config{Logger: newLogger}) if err != nil { Log.Panicf("初始化MySQL连接失败, 错误信息: %v", err) }