新增目录:models,其中新增文件AppUser.go
package models
import (
"image"
"time"
"gorm.io/gorm"
)
type AppUser struct {
gorm.Model
Username string
Password string
Nikename string
Headimg
Phone string
Logintime time.Time
Logouttime time.Time
Lastip string
}
修改main.go,新增后面8行代码
package main
import (
"fmt"
"studyApp/models"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func main() {
fmt.Println(123)
dsn := "rensoft:rensoft12345@tcp(127.0.0.1:3306)/rensoft?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
// 迁移表创建对应关系
db.AutoMigrate(&models.AppUser{})
db.Debug().Create(&models.AppUser{Username: "测试用户", Logintime: time.Now(), Logouttime: time.Now()}) // 添加记录
}
运行main.go
查看数据库结果
添加数据成功!