蒋吉兆
3 years ago
9 changed files with 125 additions and 40 deletions
-
15server/config.yaml
-
1server/config/config.go
-
14server/config/gorm.go
-
39server/initialize/gorm.go
-
39server/initialize/gorm_mysql.go
-
36server/initialize/gorm_pgsql.go
-
1server/model/system/request/sys_init.go
-
10server/resource/template/server/service.go.tpl
-
10server/service/system/sys_initdb.go
@ -0,0 +1,39 @@ |
|||
package initialize |
|||
|
|||
import ( |
|||
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|||
"gorm.io/driver/mysql" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
//@author: SliverHorn
|
|||
//@function: GormMysql
|
|||
//@description: 初始化Mysql数据库
|
|||
//@return: *gorm.DB
|
|||
|
|||
func GormMysql() *gorm.DB { |
|||
m := global.GVA_CONFIG.Mysql |
|||
if m.Dbname == "" { |
|||
return nil |
|||
} |
|||
dsn := m.Username + ":" + m.Password + "@tcp(" + m.Path + ")/" + m.Dbname + "?" + m.Config |
|||
mysqlConfig := mysql.Config{ |
|||
DSN: dsn, // DSN data source name
|
|||
DefaultStringSize: 191, // string 类型字段的默认长度
|
|||
DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持
|
|||
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引
|
|||
DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列
|
|||
SkipInitializeWithVersion: false, // 根据版本自动配置
|
|||
} |
|||
if db, err := gorm.Open(mysql.New(mysqlConfig), gormConfig()); err != nil { |
|||
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
|
|||
//os.Exit(0)
|
|||
//return nil
|
|||
return nil |
|||
} else { |
|||
sqlDB, _ := db.DB() |
|||
sqlDB.SetMaxIdleConns(m.MaxIdleConns) |
|||
sqlDB.SetMaxOpenConns(m.MaxOpenConns) |
|||
return db |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
package initialize |
|||
|
|||
import ( |
|||
"github.com/flipped-aurora/gin-vue-admin/server/global" |
|||
"gorm.io/driver/postgres" |
|||
"gorm.io/gorm" |
|||
) |
|||
|
|||
//@author: SliverHorn
|
|||
//@function: GormMysql
|
|||
//@description: 初始化Mysql数据库
|
|||
//@return: *gorm.DB
|
|||
|
|||
func GormPgSql() *gorm.DB { |
|||
p := global.GVA_CONFIG.Pgsql |
|||
if p.Dbname == "" { |
|||
return nil |
|||
} |
|||
|
|||
dsn := "host=" + p.Path + " user=" + p.Username + " password=" + p.Password + " dbname=" + p.Dbname + " port=" + p.Port + " " + p.Config |
|||
pgsqlConfig := postgres.Config{ |
|||
DSN: dsn, // DSN data source name
|
|||
PreferSimpleProtocol: false, |
|||
} |
|||
if db, err := gorm.Open(postgres.New(pgsqlConfig), gormConfig()); err != nil { |
|||
//global.GVA_LOG.Error("MySQL启动异常", zap.Any("err", err))
|
|||
//os.Exit(0)
|
|||
//return nil
|
|||
return nil |
|||
} else { |
|||
sqlDB, _ := db.DB() |
|||
sqlDB.SetMaxIdleConns(p.MaxIdleConns) |
|||
sqlDB.SetMaxOpenConns(p.MaxOpenConns) |
|||
return db |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue