From 5e41df7dc2fe33a0c9ede57209fed50e430ea82d Mon Sep 17 00:00:00 2001 From: Blizzard Date: Sat, 26 Apr 2025 11:58:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=87=AA=E5=8A=A8=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E8=A1=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config-dev.yaml | 2 +- config/zap.go | 2 +- global/model.go | 26 ++++++++++++++++++++++++++ initialize/gorm.go | 16 ++++++++++++++++ initialize/gorm_mysql.go | 13 ++++++------- main.go | 16 ++++++++++++++++ model/system/sys_user.go | 11 +++++++++++ 7 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 global/model.go create mode 100644 model/system/sys_user.go diff --git a/config-dev.yaml b/config-dev.yaml index a17b714..11f13ae 100644 --- a/config-dev.yaml +++ b/config-dev.yaml @@ -13,7 +13,7 @@ mysql: host: 127.0.0.1 port: "3306" prefix: "sundynix-" - singular: false + singular: true user: root password: root diff --git a/config/zap.go b/config/zap.go index 6ee8b8b..068ded3 100644 --- a/config/zap.go +++ b/config/zap.go @@ -33,7 +33,7 @@ func (c *Zap) Levels() []zapcore.Level { level = zapcore.DebugLevel } - // 从解析的(或默认的)日志级别开始,迭代到 FatalLevel,并将每个级别追加到切片中。 + // 从解析的(或默认的)日志级别开始,迭代到 FatalLevel,并将每个级别追加到切片中 按照日志级别分片存储 for ; level <= zapcore.FatalLevel; level++ { levels = append(levels, level) } diff --git a/global/model.go b/global/model.go new file mode 100644 index 0000000..df78a4d --- /dev/null +++ b/global/model.go @@ -0,0 +1,26 @@ +package global + +import ( + "gorm.io/gorm" + "time" +) + +type BaseModel struct { + ID uint `gorm:"primarykey" json:"ID"` // 主键ID + CreatedAt time.Time // 创建时间 + UpdatedAt time.Time // 更新时间 + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间 +} + +// BeforeCreate 定义一个钩子,在创建之前执行自动插入字段 +func (model BaseModel) BeforeCreate(db *gorm.DB) (err error) { + db.Statement.SetColumn("created_at", time.Now()) + db.Statement.SetColumn("updated_at", time.Now()) + return +} + +// BeforeUpdate 定义一个钩子,在更新之前执行自动更新字段 +func (model BaseModel) BeforeUpdate(db *gorm.DB) (err error) { + db.Statement.SetColumn("updated_at", time.Now()) + return +} diff --git a/initialize/gorm.go b/initialize/gorm.go index 5679d03..848c31e 100644 --- a/initialize/gorm.go +++ b/initialize/gorm.go @@ -1,8 +1,11 @@ package initialize import ( + "go.uber.org/zap" "gorm.io/gorm" + "os" "sundynix-go/global" + "sundynix-go/model/system" ) // Gorm 根据全局配置中的数据库类型返回对应的 *gorm.DB 实例。 @@ -22,3 +25,16 @@ func Gorm() *gorm.DB { return GormMysql() // 默认返回 MySQL 数据库的 *gorm.DB 实例 } } + +// MigrateTable 创建数据库表结构。 +func MigrateTable() { + db := global.DB + err := db.AutoMigrate( + system.User{}, + ) + if err != nil { + global.Logger.Error("Migrate table failed,err:", zap.Error(err)) + os.Exit(0) + } + global.Logger.Info("Migrate table success") +} diff --git a/initialize/gorm_mysql.go b/initialize/gorm_mysql.go index ea64a45..cc18ea9 100644 --- a/initialize/gorm_mysql.go +++ b/initialize/gorm_mysql.go @@ -5,6 +5,7 @@ import ( "gorm.io/gorm" "sundynix-go/config" "sundynix-go/global" + "sundynix-go/initialize/internal" ) // GormMysql 初始化Mysql数据库 @@ -28,15 +29,13 @@ func initMysqlDatabase(m config.Mysql) *gorm.DB { DefaultStringSize: 191, // 默认字符串类型长度 SkipInitializeWithVersion: false, // 根据版本自动配置 } - if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{ - DisableForeignKeyConstraintWhenMigrating: true, // 禁用自动创建外键约束 - }); err != nil { - return nil + if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil { + panic(err) } else { db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine) - sqlDB, _ := db.DB() - sqlDB.SetMaxIdleConns(m.MaxIdleConns) - sqlDB.SetMaxOpenConns(m.MaxOpenConns) + sqlDb, _ := db.DB() + sqlDb.SetMaxIdleConns(m.MaxIdleConns) + sqlDb.SetMaxOpenConns(m.MaxOpenConns) global.Logger.Info("Mysql connect success") return db } diff --git a/main.go b/main.go index 3e94fc6..a19bbc5 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "context" + "database/sql" "go.uber.org/zap" "sundynix-go/core" "sundynix-go/global" @@ -18,5 +20,19 @@ func main() { global.DB = initialize.Gorm() //redis连接 initialize.Redis() + global.Redis.Set(context.Background(), "test", 0, -1) + + //迁移数据库 + if global.DB != nil { + initialize.MigrateTable() // 迁移数据库结构 + db, _ := global.DB.DB() + defer func(db *sql.DB) { + err := db.Close() + if err != nil { + global.Logger.Error("db close failed", zap.Error(err)) + } + }(db) + + } } diff --git a/model/system/sys_user.go b/model/system/sys_user.go new file mode 100644 index 0000000..db58014 --- /dev/null +++ b/model/system/sys_user.go @@ -0,0 +1,11 @@ +package system + +import "sundynix-go/global" + +type User struct { + global.BaseModel + ClientId string `gorm:"size:20;" json:"clientId"` + Account string `gorm:"size:11;unique;" json:"account" form:"account"` + Password string `gorm:"size:32;" json:"-" form:"password"` + Phone string `gorm:"size:11;" json:"phone" form:"phone"` +}