Cron_Admin/app/utils/excute/gorm.go

39 lines
777 B
Go

package excute
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func NewDb(str string) (*gorm.DB, error) {
db, err := gorm.Open(
mysql.Open(str+""),
&gorm.Config{
NamingStrategy: schema.NamingStrategy{SingularTable: true},
},
)
if err != nil {
return nil, fmt.Errorf("数据库连接失败,%v", err.Error())
}
db = db.Debug()
sqlDB, _ := db.DB()
sqlDB.SetMaxIdleConns(2) //设置连接池,空闲
return db, nil
}
func ExecuteDbConn(str string) (*gorm.DB, error) {
db, err := gorm.Open(
mysql.Open(str+""),
&gorm.Config{
NamingStrategy: schema.NamingStrategy{SingularTable: true},
},
)
if err != nil {
return nil, fmt.Errorf("数据库连接失败,%v", err.Error())
}
db = db.Debug()
return db, nil
}