説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

db_bootstrapper.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dbstart
  2. import (
  3. "log"
  4. "git.x2erp.com/qdy/go-base/config"
  5. "git.x2erp.com/qdy/go-db/factory/database"
  6. )
  7. // DBBootstrapper 数据库启动器
  8. type DBBootstrapper struct {
  9. DBFactory *database.DBFactory
  10. cfg config.IConfig
  11. }
  12. // NewDBBootstrapper 创建数据库启动器
  13. func NewDBBootstrapper(cfg config.IConfig) *DBBootstrapper {
  14. return &DBBootstrapper{
  15. cfg: cfg,
  16. }
  17. }
  18. // Init 初始化数据库
  19. func (db *DBBootstrapper) Init() *DBBootstrapper {
  20. if db.cfg == nil {
  21. log.Fatal("配置未初始化,请先传入配置")
  22. }
  23. dbCfg := db.cfg.GetDatabase()
  24. log.Printf("正在连接数据库: %s:%d/%s",
  25. dbCfg.Host, dbCfg.Port, dbCfg.Database)
  26. dbFactory, err := database.GetDBFactory()
  27. if err != nil {
  28. log.Fatalf("数据库连接失败: %v", err)
  29. }
  30. db.DBFactory = dbFactory
  31. log.Println("数据库连接成功")
  32. return db
  33. }
  34. // Close 关闭数据库连接
  35. func (db *DBBootstrapper) Close() {
  36. if db.DBFactory != nil {
  37. db.DBFactory.Close()
  38. log.Println("数据库连接已关闭")
  39. }
  40. }
  41. // GetDBFactory 获取数据库工厂
  42. func (db *DBBootstrapper) GetDBFactory() *database.DBFactory {
  43. return db.DBFactory
  44. }