package dao import ( "context" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/go-xorm/xorm" "github.com/gogf/gf/v2/frame/g" ) type Dao struct { db *xorm.Engine } // var dao *xorm.Engine const ( UserName = "root" PassWord = "kepaiteng!QAZ" Host = "222.73.129.15" Port = "31306" Database = "neckring" //Charset = "utf8" ) func New() (dao *Dao) { confMap, _ := g.Cfg().Get(context.Background(), "database") sqlStr := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", confMap.Map()["UserName"], confMap.Map()["PassWord"], confMap.Map()["Host"], confMap.Map()["Port"], confMap.Map()["Database"]) // var err error xe, err := xorm.NewEngine("mysql", sqlStr) if err != nil { fmt.Println("数据库连接失败:", err) return } if xe == nil { fmt.Println("引擎初始化异常") return } dao = &Dao{ db: xe, } xe.ShowSQL(true) err = SyncTable(xe) if err != nil { fmt.Println("数据表同步失败:", err) } return }