A:数据库初始化,并增加User表示例

This commit is contained in:
jimmy committed 2025-06-15 17:37:31 +08:00
1 parent da2222f2d9
commit b8dc199cff
15 files changed
+1459 -108

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
package model
import (
"gorm.io/gorm"
"time"
)
// BaseModel 基础模型,包含通用字段
type BaseModel struct {
ID uint `gorm:"primaryKey" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
IsDeleted bool `json:"is_deleted"`
}
-26
View File
@@ -1,26 +0,0 @@
package model
import (
"rear/internal/utils"
"time"
)
// Config 配置结构
type Config struct {
Port string
Mode string
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
}
// InitConfig 初始化配置
func InitConfig() *Config {
return &Config{
Port: utils.GetEnv("PORT", "8080"),
Mode: utils.GetEnv("GIN_MODE", "debug"),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
}
+25 -4
View File
@@ -1,8 +1,29 @@
package model
// User 示例用户结构
import "gorm.io/gorm"
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
BaseModel
Username string `gorm:"uniqueIndex;not null;size:50" json:"username"`
Email string `gorm:"uniqueIndex;not null;size:100" json:"email"`
Password string `gorm:"not null;size:255" json:"-"`
Age int `gorm:"default:0" json:"age"`
Status int `gorm:"default:1;comment:1-active,0-inactive" json:"status"`
}
// TableName 指定表名
func (User) GetTableName() string {
return "users"
}
// BeforeCreate GORM钩子,创建前执行
func (u *User) BeforeCreate(tx *gorm.DB) error {
// 这里可以添加创建前的逻辑,比如密码加密
return nil
}
// AfterCreate GORM钩子,创建后执行
func (u *User) AfterCreate(tx *gorm.DB) error {
// 这里可以添加创建后的逻辑
return nil
}