| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package dbstart
-
- import (
- "log"
-
- "git.x2erp.com/qdy/go-base/config"
- "git.x2erp.com/qdy/go-db/factory/database"
- )
-
- // DBBootstrapper 数据库启动器
- type DBBootstrapper struct {
- DBFactory *database.DBFactory
- cfg config.IConfig
- }
-
- // NewDBBootstrapper 创建数据库启动器
- func NewDBBootstrapper(cfg config.IConfig) *DBBootstrapper {
- return &DBBootstrapper{
- cfg: cfg,
- }
- }
-
- // Init 初始化数据库
- func (db *DBBootstrapper) Init() *DBBootstrapper {
- if db.cfg == nil {
- log.Fatal("配置未初始化,请先传入配置")
- }
-
- dbCfg := db.cfg.GetDatabase()
-
- log.Printf("正在连接数据库: %s:%d/%s",
- dbCfg.Host, dbCfg.Port, dbCfg.Database)
-
- dbFactory, err := database.GetDBFactory()
- if err != nil {
- log.Fatalf("数据库连接失败: %v", err)
- }
-
- db.DBFactory = dbFactory
- log.Println("数据库连接成功")
-
- return db
- }
-
- // Close 关闭数据库连接
- func (db *DBBootstrapper) Close() {
- if db.DBFactory != nil {
- db.DBFactory.Close()
- log.Println("数据库连接已关闭")
- }
- }
-
- // GetDBFactory 获取数据库工厂
- func (db *DBBootstrapper) GetDBFactory() *database.DBFactory {
- return db.DBFactory
- }
|