123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package dao
- import (
- "time"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/schema"
- "kpt.notice/apiserver/config"
- "kpt.notice/pkg/log"
- )
- type Dao struct {
- DB *gorm.DB
- }
- func New(c *config.Config) (d *Dao) {
- // dsn := "root:kepaiteng!QAZ@tcp(222.73.129.15:31306)/notice?charset=utf8&parseTime=True&loc=Local"
- db, err := gorm.Open(mysql.Open(c.DB.DSN),
- // db, err := gorm.Open(mysql.Open(dsn),
- &gorm.Config{
- NamingStrategy: schema.NamingStrategy{
- SingularTable: true,
- },
- })
- if err != nil || db == nil {
- log.Errorf("gorm.Open error: %v===%v", err, c.DB.DSN)
- return nil
- }
- sqlDB, _ := db.DB()
- // SetMaxOpenConns 设置打开数据库连接的最大数量。
- sqlDB.SetMaxOpenConns(c.DB.Active)
- // SetMaxIdleConns 设置空闲连接池中连接的最大数量
- sqlDB.SetMaxIdleConns(c.DB.Idle)
- // SetConnMaxLifetime 设置了连接可复用的最大时间。
- sqlDB.SetConnMaxLifetime(c.DB.IdleTimeout * time.Second)
- d = &Dao{
- DB: db,
- }
- return
- }
|