var (
engine *xorm.Engine
dbErr error
)
func MysqlEngine() *xorm.Engine {
if engine != nil {
return engine
}
//生成连接
dataSourceName := config.InitConfig.Mysql.User + ":" + config.InitConfig.Mysql.Pwd + "@tcp(" + config.InitConfig.Mysql.Host + ":" + config.InitConfig.Mysql.Port + ")/" + config.InitConfig.Mysql.Database + "?charset=" + config.InitConfig.Mysql.Charset
//连接数据库,那种数据库加账号密码
engine, dbErr = xorm.NewEngine(config.InitConfig.Mysql.Drive, dataSourceName)
if dbErr != nil {
panic(dbErr.Error())
}
//表映射,为表名添加表头
taMapper := core.NewPrefixMapper(core.SnakeMapper{}, config.InitConfig.Mysql.Prefix_)
engine.SetTableMapper(taMapper)
显示sql语句,设置为t就行默认不显示
engine.ShowSQL(config.InitConfig.Mysql.ShowSql)
设置log等级
engine.Logger().SetLevel(config.InitConfig.Mysql.LogLevel)
设
设置池的最大空闲连接,默认为2
engine.SetMaxIdleConns(config.InitConfig.Mysql.ConnectPoolNum)
最多连接池
engine.SetMaxOpenConns(config.InitConfig.Mysql.ConnectOpenNum) return engine }