### **根据下图创建模型文件** ![](https://img.kancloud.cn/72/d8/72d8d3e65ed0c5aaaeb2af23b157f7d0_314x196.png) baseModel.go //是基础模型 (为了实现模型之间的继承 处理一下公共方法) ~~~ package models import ( "github.com/gin-gonic/gin" ) type BaseModel struct { } // **********************以下代码可以不用要******************************* type Paginate struct { Page int `form:"page" json:"page"` Limit int `form:"limit" json:"limit"` Offset int `form:"offset" json:"offset"` } func (BaseModel) ExecutePage(ctx *gin.Context) *Paginate { var paginate Paginate if ctx.ShouldBind(&paginate) == nil { paginate.Offset = (paginate.Page - 1) * paginate.Limit } return &paginate } ~~~ staff.go ~~~ package models import ( "github.com/gin-gonic/gin" "learn_gin/common" "learn_gin/db" "strconv" ) // Staff 结构体封装字段 type Staff struct { BaseModel Id int `json:"id"` Status int `json:"status"` InfoId int `json:"info_id"` Nickname string `json:"nickname"` Account string `json:"account"` Password string `json:"password"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } // TableName 该模型链接的数据表名 func (Staff) TableName() string { return "staff" } // Condition 搜索条件 func (Staff)Condition(ctx *gin.Context) string { where := "1=1" nickname := ctx.DefaultQuery("nickname","") account:= ctx.DefaultQuery("account","") status:= ctx.DefaultQuery("status","") if status != ""{ where += " AND `status` = " + status } if nickname != ""{ where += " AND `nickname` like " + status + "%" } if account != ""{ where += " AND `account` like " + account + "%" } return where } // List 查询列表 func(Staff) List(condition string,page Paginate) map[string]interface{} { var result []Staff //result := make([]models.Staff, 0) var total int64 if page.Limit != 0 || page.Offset != 0{ db.DB.Table("staff").Where(condition).Limit(page.Limit).Offset(page.Offset).Find(&result) db.DB.Table("staff").Where(condition).Limit(page.Limit).Offset(page.Offset).Count(&total) }else{ db.DB.Table("staff").Where(condition).Find(&result) db.DB.Table("staff").Where(condition).Count(&total) } data := make(map[string]interface{}) data["data"] = result data["total"] = total return data } // Insert 数据添加 func (Staff) Insert(ctx *gin.Context) { //data := &Staff{} var data Staff status ,_ := strconv.ParseInt(ctx.PostForm("status"),10,8) infoId,_ := strconv.ParseInt(ctx.PostForm("info_id"),10,8) data.Status = int(status) data.Nickname = ctx.PostForm("nickname") data.Account = ctx.PostForm("nickname") data.InfoId = int(infoId) data.Password = common.Md5(ctx.PostForm("password")) data.CreatedAt = common.GetDate() data.UpdatedAt = common.GetDate() db.DB.Table("staff").Create(&data) } func (Staff) Update(ctx *gin.Context) { var data Staff staffId := ctx.DefaultQuery("staff_id","") status ,_ := strconv.ParseInt(ctx.PostForm("status"),10,8) infoId,_ := strconv.ParseInt(ctx.PostForm("info_id"),10,8) where := "`id` = " + staffId password := ctx.DefaultQuery("password","") if password != ""{ password = common.Md5(password) } data.Status = int(status) data.Nickname = ctx.PostForm("nickname") data.Account = ctx.PostForm("nickname") data.InfoId = int(infoId) data.Password = password data.UpdatedAt = common.GetDate() db.DB.Table("staff").Where(where).Updates(&data) } ~~~