|
|
package DbUtil
|
|
|
|
|
|
import (
|
|
|
"dsSupport/Const/ErrorConst"
|
|
|
"dsSupport/Utils/ConfigUtil"
|
|
|
"dsSupport/Utils/LogUtil"
|
|
|
"fmt"
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
"github.com/xormplus/core"
|
|
|
"github.com/xormplus/xorm"
|
|
|
"github.com/xormplus/xorm/log"
|
|
|
"os"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
var Engine *xorm.EngineGroup
|
|
|
|
|
|
func init() {
|
|
|
host := ConfigUtil.MysqlIp
|
|
|
port := ConfigUtil.MysqlPort
|
|
|
user := ConfigUtil.MysqlUser
|
|
|
dbname := ConfigUtil.MysqlDataBase
|
|
|
password := ConfigUtil.MysqlPwd
|
|
|
var conns []string
|
|
|
//mysql
|
|
|
master := fmt.Sprintf("%s:%s@%s(%s:%s)/%s?charset=utf8", user, password, "tcp", host, port, dbname)
|
|
|
conns = append(conns, master)
|
|
|
var err error
|
|
|
Engine, err = xorm.NewEngineGroup("mysql", conns)
|
|
|
if err != nil {
|
|
|
LogUtil.Error(ErrorConst.SqlQueryError, err.Error())
|
|
|
}
|
|
|
//设置数据库连接池
|
|
|
Engine.SetMaxIdleConns(200) //设置连接池中的保持连接的最大连接数。
|
|
|
Engine.SetMaxOpenConns(200) //设置打开数据库的最大连接数,包含正在使用的连接和连接池的连接。
|
|
|
Engine.SetConnMaxLifetime(time.Minute * 5)
|
|
|
//设置数据时区
|
|
|
var location *time.Location
|
|
|
location, _ = time.LoadLocation("Asia/Shanghai")
|
|
|
Engine.TZLocation = location
|
|
|
//与 struct的映射方式,这里采用蛇型方法,默认是蛇形
|
|
|
Engine.SetTableMapper(core.SnakeMapper{})
|
|
|
|
|
|
//显示+记录SQL日志
|
|
|
f, _ := os.Create("./Logs/sql.log")
|
|
|
|
|
|
logger := log.NewSimpleLogger(f)
|
|
|
logger.SetLevel(log.LOG_DEBUG)
|
|
|
Engine.SetLogger(log.NewLoggerAdapter(logger))
|
|
|
Engine.ShowSQL(true) // 则会在控制台打印出生成的SQL语句
|
|
|
}
|
|
|
|