1
0
forked from lxh/go-wxhelper
go-wxhelper/client/mysql.go

35 lines
743 B
Go
Raw Normal View History

2023-09-21 17:33:59 +08:00
package client
import (
"go-wechat/config"
2023-09-21 17:33:59 +08:00
"gorm.io/driver/mysql"
"gorm.io/gorm"
2023-12-07 23:00:16 +08:00
"gorm.io/gorm/logger"
2023-09-21 17:33:59 +08:00
"log"
)
// MySQL客户端
var MySQL *gorm.DB
func InitMySQLClient() {
// 创建连接对象
mysqlConfig := mysql.Config{
DSN: config.Conf.MySQL.GetDSN(),
2023-09-21 17:33:59 +08:00
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
DontSupportRenameColumn: true, // 用 `change` 重命名列
}
2023-12-07 23:00:16 +08:00
// gorm 配置
gormConfig := gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
}
conn, err := gorm.Open(mysql.New(mysqlConfig), &gormConfig)
2023-09-21 17:33:59 +08:00
if err != nil {
log.Panicf("初始化MySQL连接失败, 错误信息: %v", err)
} else {
log.Println("MySQL连接成功")
}
MySQL = conn
}