连接数据库公共方法
package database import ( "Ganzhou/config" "Ganzhou/pkg/log" "Ganzhou/pkg/util/security" "database/sql" "fmt" "github.com/cengsin/oracle" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "gorm.io/gorm/schema" logs "log" "os" "time" ) var ( DB *gorm.DB ) const DBEncryptKey = "EMd3EKXwLACDupbz" // 长度必须是16, 24, 32 , func init() { newLogger := logger.New( logs.New(os.Stdout, "\r\n", logs.LstdFlags), // io writer logger.Config{ SlowThreshold: time.Second, // 慢 SQL 阈值 LogLevel: logger.Info, // Log level Colorful: true, // 不禁用彩色打印 }, ) ormConfig := &gorm.Config{ //设置全局表名禁用复数 NamingStrategy: schema.NamingStrategy{ //TablePrefix: "t_", // 表名前缀,`User` 的表名应该是 `t_users` SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user` }, } // debug 设置打印日志 if config.AppMode == "debug" { ormConfig.Logger = newLogger } var err error aesCfb := security.NewAESCFBEncrypt(DBEncryptKey) masterPassword := aesCfb.Decrypt(config.MasterDbPassWord) err = aesCfb.Err if err != nil { log.Errorf("Decrypt SignKey error:%v", err.Error()) log.Fatal("Decrypt SignKey error: %v", err.Error()) } masterDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", config.MasterDbUserName, masterPassword, config.MasterDbHost, config.MasterDbPort, config.MasterDbName, // 数据库 ) DB, err = gorm.Open(mysql.Open(masterDsn), ormConfig) if err != nil { log.Errorf("连接mysql数据库失败:", config.MasterDbHost, err) log.Fatal("连接mysql数据库失败 error: %v", err.Error()) } sqlDB, err := DB.DB() sqlDB.SetMaxIdleConns(config.OrmMaxidleconns) sqlDB.SetMaxOpenConns(config.OrmMaxopenconns) // invalid connection 这个是由于mysql数据库链接有超时设置 导致go连接池使用了无效连接导致bug sqlDB.SetConnMaxLifetime(time.Duration(config.OrmMaxlifetime) * time.Second) } var ( OracleDB *gorm.DB OrlDB *sql.DB ) func init() { newLogger := logger.New( logs.New(os.Stdout, "\r\n", logs.LstdFlags), // io writer logger.Config{ SlowThreshold: time.Second, // 慢 SQL 阈值 LogLevel: logger.Info, // Log level Colorful: true, // 不禁用彩色打印 }, ) ormConfig := &gorm.Config{ //设置全局表名禁用复数 NamingStrategy: schema.NamingStrategy{ //TablePrefix: "t_", // 表名前缀,`User` 的表名应该是 `t_users` SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user` }, } // debug 设置打印日志 if config.AppMode == "debug" { ormConfig.Logger = newLogger } var err error //OracleDB, err = gorm.Open(oracle.Open("system/123456@192.168.20.39:1521/test"), ormConfig) //OracleDB, err = gorm.Open(oracle.Open("tyyl_dxgw/tyyl_dxgw@192.168.10.8:1521/TYYL"), ormConfig) OracleDB, err = gorm.Open(oracle.Open("tyyl_dxgw/tyyl_dxgw@61.134.67.58:1521/TYYL"), ormConfig) if err != nil { //log.Errorf("连接oracle数据库失败:", config.DbHost, err) log.Fatal("连接oracle数据库失败 error: %v", err.Error()) } //err = DB.Ping() //OrlDB = DB log.Info("链接成功!") }
方法调用
// 查询申请数据 func PutFun() { var list []*YjM.HealthSampleHandle err := database.DB.Model(&YjM.HealthSampleHandle{}). Select("health_sample.*,(select respDr_name from health_check where health_check.id=health_sample.id ) respDr_name,(select name_path from sys_department where nc_code=region_code )name_path"). //Where("id in ('fd0da63a9ad7498b77d774b69909a12e','1d82cac28ac84b1c4cf27cb089174754','f87fd000113045b54dd88c36a414689a','e3a8c65175e048e65282e595c3c0f7b5')"). Where("nc_code like '620702%' and status=0 AND status_gn=0 "). Where("check_date BETWEEN '2023-11-01' and '2023-11-17'"). Find(&list).Error if err == nil { for _, hs := range list { //pData := &model.PutRequest{} //PutRdate(hs, pData) //date := PutPostRdoteFun(*pData) //PutPostFun(date, hs.Id, pData.ApplyNo) PostListFun(hs) } select {} } }