上一篇文章goframe项目详解(基于开源项目 易理解)(二)-CSDN博客介绍完了APP目录下的dao文件,本文我们来介绍开源项目的model目录
model 里定义的结构体通常可以类比于 Java 中的实体类(Entity Class)或数据传输对象(Data Transfer Object,DTO)
前面的把interal目录下的category讲解了,除了category还有content file interact reply setting user
再看看model目录下有什么?比dao目录下多了captcha.go context.go menu.go model.go session.go view.go
我们一个一个的看:captcha.go “captcha”常见释义为“验证码;验证图”。
package model
const (
CaptchaDefaultName = "CaptchaDefaultName"
)
在 model 包中定义了一个常量 CaptchaDefaultName ,其值为 "CaptchaDefaultName" 。
咱们接着看category.go
package model
// 栏目树形列表
type CategoryTreeItem struct {
Id uint `json:"id"` // 分类ID,自增主键
ParentId uint `json:"parent_id"` // 父级分类ID,用于层级管理
Name string `json:"name"` // 分类名称
Thumb string `json:"thumb"` // 封面图
Brief string `json:"brief"` // 简述
Content string `json:"content"` // 详细介绍
Indent string `json:"indent"` // 缩进字符串,包含: , " │", " ├", " └"
Items []*CategoryTreeItem `json:"items,omitempty"` // 子级数据项
}
定义了一个名为 CategoryTreeItem 的结构体,用于表示栏目树形列表中的一个节点。
每个节点包含了诸如 Id(分类 ID)、ParentId(父级分类 ID)、Name(分类名称)等信息,还包含了一个可能为空的子节点切片 Items 。
如果你看过我之前的博客,你会发现在dao层也定义了一个catagory的结构体,那么这个结构体,那么这连个结构体有什么区别呢?
CategoryTreeItem 是一个用于表示栏目树形列表节点的结构体,它定义了节点所包含的各种信息,如分类 ID、父级分类 ID、名称等。
model 包中的 CategoryTreeItem 结构体主要用于定义数据的结构和表示形式。
它描述了一个栏目树形列表中每个节点所应包含的各种属性,比如 Id、ParentId、Name 等。
这种结构体的定义有助于在整个应用程序中保持数据的一致性和规范性。在不同的业务逻辑中,可以创建、操作和传递 CategoryTreeItem 类型的变量来处理和表示栏目相关的数据。
例如,在数据获取、处理、展示等环节,都可以基于这个结构体来进行数据的组织和交互。
而 CategoryDao 结构体及相关的代码则是数据访问对象(Data Access Object,DAO)的实现,用于处理与数据库的交互操作。
CategoryDao 结构体中的成员具有以下作用:
gmvc.M:继承了一些数据库操作的方法和功能。C(categoryColumns类型):用于定义和存储与表gf_category列名相关的信息,方便在操作数据库时使用这些列名。DB(gdb.DB类型):表示底层的数据库管理对象,用于执行实际的数据库操作。Table:指定了所操作的表名为gf_category。
通过 NewCategoryDao 函数创建 CategoryDao 对象时,进行了一些初始化设置,例如设置了列名、指定了默认的数据库连接等。
我们接着看:menu.go
package model
// 菜单数据结构
type MenuItem struct {
Name string // 显示名称
Url string // 链接地址
Icon string // 图标,可能是class,也可能是iconfont
Target string // 打开方式: 空, _blank
Active bool // 是否被选中
Items []*MenuItem // 子级菜单
}
在 model 包中定义了一个名为 MenuItem 的结构体,用于表示菜单项目。
MenuItem 结构体包含了菜单的名称、链接地址、图标、打开方式、是否活跃(被选中)的状态,以及一个包含子菜单的切片。
这样的结构体定义有助于在程序中组织和处理菜单相关的数据,例如构建菜单树、进行菜单的渲染和操作等。
Items []*MenuItem 是一个切片。
在这个结构体定义中,Items 是一个元素类型为 *MenuItem(即指向 MenuItem 结构体的指针)的切片。这意味着它可以存储多个指向 MenuItem 结构体的指针。
我问你,如果换成数组怎么表示?
Items [N]*MenuItem N代表具体的数字
我们接着看:model.go
// =================================================================================
// Code generated by GoFrame CLI tool. DO NOT EDIT.
// =================================================================================
package model
import (
"github.com/gogf/gf/os/gtime"
)
// Category is the golang structure for table gf_category.
type Category struct {
Id uint `orm:"id,primary" json:"id"` // 分类ID,自增主键
ContentType string `orm:"content_type" json:"contentType"` // 内容类型:topic, ask, article, reply
Key string `orm:"key" json:"key"` // 栏目唯一键名,用于程序部分场景硬编码,一般不会用得到
ParentId uint `orm:"parent_id" json:"parentId"` // 父级分类ID,用于层级管理
UserId uint `orm:"user_id" json:"userId"` // 创建的用户ID
Name string `orm:"name" json:"name"` // 分类名称
Sort uint `orm:"sort" json:"sort"` // 排序,数值越低越靠前,默认为添加时的时间戳,可用于置顶
Thumb string `orm:"thumb" json:"thumb"` // 封面图
Brief string `orm:"brief" json:"brief"` // 简述
Content string `orm:"content" json:"content"` // 详细介绍
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 创建时间
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 修改时间
}
// Content is the golang structure for table gf_content.
type Content struct {
Id uint `orm:"id,primary" json:"id"` // 自增ID
Key string `orm:"key" json:"key"` // 唯一键名,用于程序硬编码,一般不常用
Type string `orm:"type" json:"type"` // 内容模型: topic, ask, article等,具体由程序定义
CategoryId uint `orm:"category_id" json:"categoryId"` // 栏目ID
UserId uint `orm:"user_id" json:"userId"` // 用户ID
AdoptedReplyId uint `orm:"adopted_reply_id" json:"adoptedReplyId"` // 采纳的回复ID,问答模块有效
Title string `orm:"title" json:"title"` // 标题
Content string `orm:"content" json:"content"` // 内容
Sort uint `orm:"sort" json:"sort"` // 排序,数值越低越靠前,默认为添加时的时间戳,可用于置顶
Brief string `orm:"brief" json:"brief"` // 摘要
Thumb string `orm:"thumb" json:"thumb"` // 缩略图
Tags string `orm:"tags" json:"tags"` // 标签名称列表,以JSON存储
Referer string `orm:"referer" json:"referer"` // 内容来源,例如github/gitee
Status uint `orm:"status" json:"status"` // 状态 0: 正常, 1: 禁用
ReplyCount uint `orm:"reply_count" json:"replyCount"` // 回复数量
ViewCount uint `orm:"view_count" json:"viewCount"` // 浏览数量
ZanCount uint `orm:"zan_count" json:"zanCount"` // 赞
CaiCount uint `orm:"cai_count" json:"caiCount"` // 踩
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 创建时间
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 修改时间
}
// File is the golang structure for table gf_file.
type File struct {
Id uint `orm:"id,primary" json:"id"` // 自增ID
Name string `orm:"name" json:"name"` // 文件名称
Src string `orm:"src" json:"src"` // 本地文件存储路径
Url string `orm:"url" json:"url"` // URL地址,可能为空
UserId uint `orm:"user_id" json:"userId"` // 操作用户
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` //
}
// Interact is the golang structure for table gf_interact.
type Interact struct {
Id uint `orm:"id,primary" json:"id"` // 自增ID
Type int `orm:"type" json:"type"` // 操作类型。0:赞,1:踩。
UserId uint `orm:"user_id" json:"userId"` // 操作用户
TargetId uint `orm:"target_id" json:"targetId"` // 对应内容ID,该内容可能是content, reply
TargetType string `orm:"target_type" json:"targetType"` // 内容模型: content, reply, 具体由程序定义
Count uint `orm:"count" json:"count"` // 操作数据值
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` //
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` //
}
// Reply is the golang structure for table gf_reply.
type Reply struct {
Id uint `orm:"id,primary" json:"id"` // 回复ID
ParentId uint `orm:"parent_id" json:"parentId"` // 回复对应的上一级回复ID(没有的话默认为0)
Title string `orm:"title" json:"title"` // 回复标题
Content string `orm:"content" json:"content"` // 回复内容
TargetType string `orm:"target_type" json:"targetType"` // 评论类型: content, reply
TargetId uint `orm:"target_id" json:"targetId"` // 对应内容ID,可能回复的是另一个回复,所以这里没有使用content_id
UserId uint `orm:"user_id" json:"userId"` // 网站用户ID
ZanCount uint `orm:"zan_count" json:"zanCount"` // 赞
CaiCount uint `orm:"cai_count" json:"caiCount"` // 踩
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 创建时间
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` //
}
// Setting is the golang structure for table gf_setting.
type Setting struct {
K string `orm:"k,primary" json:"k"` // 键名
V string `orm:"v" json:"v"` // 键值
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 创建时间
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新时间
}
// User is the golang structure for table gf_user.
type User struct {
Id uint `orm:"id,primary" json:"id"` // UID
Passport string `orm:"passport,unique" json:"passport"` // 账号
Password string `orm:"password" json:"password"` // MD5密码
Nickname string `orm:"nickname,unique" json:"nickname"` // 昵称
Avatar string `orm:"avatar" json:"avatar"` // 头像地址
Status int `orm:"status" json:"status"` // 状态 0:启用 1:禁用
Gender int `orm:"gender" json:"gender"` // 性别 0: 未设置 1: 男 2: 女
CreatedAt *gtime.Time `orm:"created_at" json:"createdAt"` // 注册时间
UpdatedAt *gtime.Time `orm:"updated_at" json:"updatedAt"` // 更新时间
}
定义了一系列与数据库表对应的结构体,用于表示不同表中的数据结构。
每个结构体都包含了与对应表的字段相对应的成员变量,并通过 orm 标签可能指定了一些与对象关系映射(ORM)相关的属性,以及 json 标签用于在序列化到 JSON 时指定字段的名称。
接下来看session.go
package model
const (
SessionNoticeTypeSuccess = "success"
SessionNoticeTypeInfo = "primary"
SessionNoticeTypeWarn = "warning"
SessionNoticeTypeError = "danger"
)
// 存放在Session中的提示信息,往往使用后则删除
type SessionNotice struct {
Type string // 消息类型
Content string // 消息内容
}
定义了一些常量和一个名为 SessionNotice 的结构体。
常量定义了不同类型的会话通知类型,如成功、信息、警告和错误。
SessionNotice 结构体包含两个字段:Type 表示通知的类型,是一个字符串;Content 表示通知的具体内容,也是一个字符串。这种结构体可能用于在程序的会话中传递和存储通知相关的信息
接下来是view.go
package model
// 视图渲染内容对象
type View struct {
Title string // 页面标题
Keywords string // 页面Keywords
Description string // 页面Description
Error string // 错误信息
MainTpl string // 自定义MainTpl展示模板文件
Redirect string // 引导页面跳转
ContentType string // 内容模型
BreadCrumb []ViewBreadCrumb // 面包屑
Data interface{} // 页面参数
}
// 视图面包屑结构
type ViewBreadCrumb struct {
Name string // 显示名称
Url string // 链接地址,当为空时表示被选中
}
定义了两个与视图相关的数据结构:View 和 ViewBreadCrumb 。
View 结构体包含了页面的标题、关键词、描述、错误信息、自定义主模板文件、重定向地址、内容模型、面包屑切片和页面参数等信息。
ViewBreadCrumb 结构体包含了面包屑的名称和链接地址。
这两个结构体有什么区别呢?
View 和 ViewBreadCrumb 这两个结构体的区别主要在于它们所承载的数据和用途:
-
View结构体:- 包含了关于整个视图页面的综合信息,如页面的标题、关键词、描述、错误信息、模板文件、重定向地址、内容模型以及用于传递给页面的通用数据
Data。 - 它是对整个页面视图状态和配置的一种抽象和封装。
- 包含了关于整个视图页面的综合信息,如页面的标题、关键词、描述、错误信息、模板文件、重定向地址、内容模型以及用于传递给页面的通用数据
-
ViewBreadCrumb结构体:- 只专注于表示面包屑导航中的一个单独的项目,包含了项目的名称和链接地址。
- 多个
ViewBreadCrumb项目组合起来形成完整的面包屑导航数据。
总的来说,View 是更宏观、全面的页面视图描述,而 ViewBreadCrumb 是其中关于面包屑导航这一特定部。
2322

被折叠的 条评论
为什么被折叠?



