package models import ( "fmt" "goftp/gormdb" "time" ) type File struct { Id string `gorm:"primaryKey;size:20"` Downloadcount int64 `gorm:"size:20" json:"downloadcount"` Name string `gorm:"size:50" json:"fileName"` LastDownloadtime time.Time `json:"last_downloadtime"` } func init() { fmt.Println("File init()") gormdb.DB.AutoMigrate(&File{}) } // TableName 会将 User 的表名重写为 `user` func (File) TableName() string { return "file" } func FindFileById(id string) (*File, error) { var file File gormdb.DB.First(&file, id) return &file, nil } func FindFileByFileName(name string) *File { var file File gormdb.DB.Where("name = ?", name).First(&file) return &file //fileModel := new(File) //err := orm.NewOrm().QueryTable(new(File)).Filter("name", name).One(fileModel) //if err != nil { // return nil, err //} //return fileModel, nil } func UpdateFile(file *File) error { result := gormdb.DB.Save(&file) if result.Error != nil { fmt.Println("Failed to UpdateFile:", result.Error) return result.Error } else { fmt.Println("UpdateFile successfully!") } return nil } func InsertFile(file *File) error { result := gormdb.DB.Save(&file) if result.Error != nil { fmt.Println("Failed to create InsertFile:", result.Error) return result.Error } else { fmt.Println("InsertFile created successfully!") } return nil }