12345678910111213141516171819202122232425262728293031 |
- package initialize
- import (
- "AIT/global"
- "fmt"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/schema"
- "time"
- )
- func Mysql() {
- mysqlConfig := global.Config.Mysql
- var dsn = fmt.Sprintf("%s:%s@%s", mysqlConfig.UserName, mysqlConfig.Password, mysqlConfig.Url)
- db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
- NamingStrategy: schema.NamingStrategy{SingularTable: true},
- })
- if err != nil {
- fmt.Printf("mysql error :%s", err.Error())
- return
- }
- sqlDb, err := db.DB()
- if err != nil {
- fmt.Printf("mysql error :%s", err.Error())
- }
- sqlDb.SetMaxIdleConns(10)
- sqlDb.SetMaxOpenConns(100)
- sqlDb.SetConnMaxLifetime(time.Hour)
- global.Db = db
- }
|