dao.go 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/go-xorm/xorm"
  7. "github.com/gogf/gf/v2/frame/g"
  8. )
  9. type Dao struct {
  10. db *xorm.Engine
  11. }
  12. // var dao *xorm.Engine
  13. const (
  14. UserName = "root"
  15. PassWord = "kepaiteng!QAZ"
  16. Host = "222.73.129.15"
  17. Port = "31306"
  18. Database = "neckring"
  19. //Charset = "utf8"
  20. )
  21. func New() (dao *Dao) {
  22. confMap, _ := g.Cfg().Get(context.Background(), "database")
  23. 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"])
  24. // var err error
  25. xe, err := xorm.NewEngine("mysql", sqlStr)
  26. if err != nil {
  27. fmt.Println("数据库连接失败:", err)
  28. return
  29. }
  30. if xe == nil {
  31. fmt.Println("引擎初始化异常")
  32. return
  33. }
  34. dao = &Dao{
  35. db: xe,
  36. }
  37. xe.ShowSQL(true)
  38. err = SyncTable(xe)
  39. if err != nil {
  40. fmt.Println("数据表同步失败:", err)
  41. }
  42. return
  43. }