diff --git a/utils/dorm/gorm.go b/utils/dorm/gorm.go index 99aba4c2..b358ba2e 100644 --- a/utils/dorm/gorm.go +++ b/utils/dorm/gorm.go @@ -30,7 +30,7 @@ type GormClientConfig struct { // GormClient // https://gorm.io/zh_CN/docs/index.html type GormClient struct { - Db *gorm.DB // 驱动 + db *gorm.DB // 驱动 config *GormClientConfig // 配置 } diff --git a/utils/dorm/gorm_get.go b/utils/dorm/gorm_get.go index 613d3bd6..9a527859 100644 --- a/utils/dorm/gorm_get.go +++ b/utils/dorm/gorm_get.go @@ -4,5 +4,5 @@ import "gorm.io/gorm" // GetDb 获取驱动 func (c *GormClient) GetDb() *gorm.DB { - return c.Db + return c.db } diff --git a/utils/dorm/gorm_mysql.go b/utils/dorm/gorm_mysql.go index 9373b365..e9400fb4 100644 --- a/utils/dorm/gorm_mysql.go +++ b/utils/dorm/gorm_mysql.go @@ -37,7 +37,7 @@ func NewGormMysqlClient(config *GormClientConfig) (*GormClient, error) { } else { logLevel = logger.Info } - c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{ + c.db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{ Logger: logger.New( writer{}, logger.Config{ @@ -52,37 +52,37 @@ func NewGormMysqlClient(config *GormClientConfig) (*GormClient, error) { }, }) } else { - c.Db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{}) + c.db, err = gorm.Open(mysql.Open(c.config.Dns), &gorm.Config{}) } if err != nil { return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) } - sqlDB, err := c.Db.DB() + sqlDd, err := c.db.DB() if err != nil { return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) } // 设置空闲连接池中连接的最大数量 if c.config.ConnSetMaxIdle == 0 { - sqlDB.SetMaxIdleConns(10) + sqlDd.SetMaxIdleConns(10) } else { - sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) + sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle) } // 设置打开数据库连接的最大数量 if c.config.ConnSetMaxOpen == 0 { - sqlDB.SetMaxOpenConns(100) + sqlDd.SetMaxOpenConns(100) } else { - sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) + sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen) } // 设置了连接可复用的最大时间 if c.config.ConnSetConnMaxLifetime == 0 { - sqlDB.SetConnMaxLifetime(time.Second * 600) + sqlDd.SetConnMaxLifetime(time.Second * 600) } else { - sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) + sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) } return c, nil diff --git a/utils/dorm/gorm_postgresql.go b/utils/dorm/gorm_postgresql.go index 8d887e4b..321febf3 100644 --- a/utils/dorm/gorm_postgresql.go +++ b/utils/dorm/gorm_postgresql.go @@ -38,7 +38,7 @@ func NewGormPostgresClient(config *GormClientConfig) (*GormClient, error) { } else { logLevel = logger.Info } - c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ + c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ Logger: logger.New( writer{}, logger.Config{ @@ -53,37 +53,37 @@ func NewGormPostgresClient(config *GormClientConfig) (*GormClient, error) { }, }) } else { - c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) + c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) } if err != nil { return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) } - sqlDB, err := c.Db.DB() + sqlDd, err := c.db.DB() if err != nil { return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) } // 设置空闲连接池中连接的最大数量 if c.config.ConnSetMaxIdle == 0 { - sqlDB.SetMaxIdleConns(10) + sqlDd.SetMaxIdleConns(10) } else { - sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) + sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle) } // 设置打开数据库连接的最大数量 if c.config.ConnSetMaxOpen == 0 { - sqlDB.SetMaxOpenConns(100) + sqlDd.SetMaxOpenConns(100) } else { - sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) + sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen) } // 设置了连接可复用的最大时间 if c.config.ConnSetConnMaxLifetime == 0 { - sqlDB.SetConnMaxLifetime(time.Hour) + sqlDd.SetConnMaxLifetime(time.Hour) } else { - sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) + sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) } return c, nil @@ -117,7 +117,7 @@ func NewGormPostgresqlClient(config *GormClientConfig) (*GormClient, error) { } else { logLevel = logger.Info } - c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ + c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{ Logger: logger.New( writer{}, logger.Config{ @@ -132,37 +132,37 @@ func NewGormPostgresqlClient(config *GormClientConfig) (*GormClient, error) { }, }) } else { - c.Db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) + c.db, err = gorm.Open(postgres.Open(c.config.Dns), &gorm.Config{}) } if err != nil { return nil, errors.New(fmt.Sprintf("连接失败:%v", err)) } - sqlDB, err := c.Db.DB() + sqlDd, err := c.db.DB() if err != nil { return nil, errors.New(fmt.Sprintf("检查连接失败:%v", err)) } // 设置空闲连接池中连接的最大数量 if c.config.ConnSetMaxIdle == 0 { - sqlDB.SetMaxIdleConns(10) + sqlDd.SetMaxIdleConns(10) } else { - sqlDB.SetMaxIdleConns(c.config.ConnSetMaxIdle) + sqlDd.SetMaxIdleConns(c.config.ConnSetMaxIdle) } // 设置打开数据库连接的最大数量 if c.config.ConnSetMaxOpen == 0 { - sqlDB.SetMaxOpenConns(100) + sqlDd.SetMaxOpenConns(100) } else { - sqlDB.SetMaxOpenConns(c.config.ConnSetMaxOpen) + sqlDd.SetMaxOpenConns(c.config.ConnSetMaxOpen) } // 设置了连接可复用的最大时间 if c.config.ConnSetConnMaxLifetime == 0 { - sqlDB.SetConnMaxLifetime(time.Hour) + sqlDd.SetConnMaxLifetime(time.Hour) } else { - sqlDB.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) + sqlDd.SetConnMaxLifetime(time.Duration(c.config.ConnSetConnMaxLifetime)) } return c, nil diff --git a/utils/dorm/gorm_transaction.go b/utils/dorm/gorm_transaction.go index d472c296..ab6cf679 100644 --- a/utils/dorm/gorm_transaction.go +++ b/utils/dorm/gorm_transaction.go @@ -4,15 +4,15 @@ import "gorm.io/gorm" // Begin 开始事务,不需要创建 Session 对象 func (c *GormClient) Begin() *gorm.DB { - return c.Db.Begin() + return c.db.Begin() } // Rollback 回滚事务 func (c *GormClient) Rollback() *gorm.DB { - return c.Db.Rollback() + return c.db.Rollback() } // Commit 提交事务 func (c *GormClient) Commit() *gorm.DB { - return c.Db.Commit() + return c.db.Commit() } diff --git a/utils/dorm/xorm_transaction.go b/utils/dorm/xorm_transaction.go index 0d83b193..cad34abb 100644 --- a/utils/dorm/xorm_transaction.go +++ b/utils/dorm/xorm_transaction.go @@ -11,7 +11,7 @@ type XormClientSession struct { // Begin 开始事务,需要创建 Session 对象 //func (c *XormClient) Begin() (*XormClientSession, error) { -// session := c.Db.NewSession() +// session := c.db.NewSession() // defer session.Close() // return &session, session.Begin() //}