这里是目录呀~最近在做一个工程原型,后端打算采用Golang的Gin框架,找到了一个比较好的Gin框架GinSkeleton。本文是我尝试使用该框架时做的实验记录。
https://gitee.com/daitougege/GinSkeleton我的实验的基础是在其样例后端框架的基础上进行的。
https://gitee.com/daitougege/gin-skeleton-admin-backend
- 第一次尝试使用[GinSkeleton](https://gitee.com/daitougege/GinSkeleton)框架
- 〇、准备数据库环境
- 1. 配置mysql环境
- 2. 修改配置文件
- 3. 测试
- 一、实验主要内容
- 1. 创建数据库
- 2. 创建model
- 3. 创建controller
- 4. 创建验证器
- 5. 注册验证器
- 6. 注册路由器
- 二、验证
- 1. 启动项目
- 2. 测试
- 参考
〇、准备数据库环境 1. 配置mysql环境
将/database下文件解压,并导入到数据库中。
-<%>- mysql -h dai -u root -p1232. 修改配置文件 修改/config/gorm_v2.yml文件,修改数据库地址和密码。
3. 测试执行 /cmd/web/main.go
可以测试一下获取验证码的链接http://127.0.0.1:20201/captcha/,可以看到已经成功读取数据库数据。
一、实验主要内容1. 创建数据库我想要完成一个查询任务分类列表的后端接口。
create table if not exists tb_category ( id int(10) unsigned not null, name varchar(20) default '' comment '名称', description varchar(1000) default '' comment '描述', file varchar(30) default '' comment '图标文件地址', primary key(id) ); insert into `tb_category`(`id`,`name`,`description`) values (1,'机器学习','描述描述描述描述描述描述描述描述描述描述'), (2,'计算机视觉','描述描述描述描述描述描述描述描述描述描述'), (3,'语音识别','描述描述描述描述描述描述描述描述描述描述'), (4,'自然语言处理','描述描述描述描述描述描述描述描述描述描述'); create table if not exists tb_task( id int(10) unsigned not null, category_id int(10) unsigned not null comment '外键: category表id', name varchar(20) default '' comment '名称', primary key(id) ); insert into `tb_task`(`id`,`name`,`category_id`) values (1,'任务一',1), (2,'任务二',1), (3,'任务三',2), (4,'任务四',4);-<1:%>- mysql -h dai -u root -p123 -D db_ginskeleton2. 创建model 在/app/http/model中创建category/category.go文件
package category import ( "go.uber.org/zap" "goskeleton/app/global/variable" "goskeleton/app/model" ) func CreatCategoryFactory(sqlType string) * CategoryModel { return &CategoryModel{BaseModel:model.BaseModel{DB:model.UseDbConn(sqlType)}} } type CategoryModel struct { model.BaseModel Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` File string `json:"file"` } // 需要返回给前端的数据,建议在同文件夹下建一个`data_type.go`的文件 type CategoryView struct { Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` File string `json:"file"` } // 表名 func (c * CategoryModel) TableName() string { return "tb_category" } // 查询列表 func (c * CategoryModel) List(limitStart,limit int) (list []CategoryView) { sql := ` select * from tb_category limit ? , ?; ` if res := c.Raw(sql, limitStart, limit).Find(&list); res.Error != nil{ variable.ZapLog.Error("CategoryModel 查询出错", zap.Error(res.Error)) } return }3. 创建controller在/app/http/controller中创建category/category.go文件
package category import ( "fmt" "github.com/gin-gonic/gin" "goskeleton/app/global/consts" "goskeleton/app/global/variable" "goskeleton/app/model/category" "goskeleton/app/utils/response" ) type Category struct { Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` File string `json:"file"` } // 添加一项 func (c *Category) AddCategory(context *gin.Context) { c.Id = uint(variable.SnowFlake.GetId()) response.Success(context, "验信息", c) } // 获取分类列表 func (c *Category) List(context *gin.Context) { var limit = context.GetFloat64(consts.ValidatorPrefix + "limit") var limitStart = (context.GetFloat64(consts.ValidatorPrefix+"page") - 1) * limit fmt.Println(limit,limitStart) list := category.CreatCategoryFactory("").List(int(limitStart),int(limit)) if list != nil{ response.Success(context, consts.CurdStatusOkMsg, gin.H{ "data": list, }) }else { response.Fail(context,consts.CurdSelectFailCode, consts.CurdSelectFailMsg,"") } }4. 创建验证器在/app/http/validator/web中创建category/list.go文件
package category import ( "github.com/gin-gonic/gin" "goskeleton/app/global/consts" "goskeleton/app/http/controller/category" "goskeleton/app/http/validator/core/data_transfer" "goskeleton/app/http/validator/web/auth/common_data_type" "goskeleton/app/utils/response" ) type List struct { common_data_type.Page } // 验证器语法 func (l List) CheckParams(context *gin.Context) { // 1. 基本的验证规则 if err := context.ShouldBind(&l); err!=nil{ errs := gin.H{ "tips": "category 参数校验失败,参数不符合规定,page的值(>0)、limits的值(>0)", "err": err.Error(), } response.ErrorParam(context,errs) return } // 该函数的主要作用是将本结构体的字段(成员)按照consts.ValidatorPrefix + json 标签对应的 键 => 值 形式直接传递给下一步 (控制器) extraAddBindDataContext := data_transfer.DataAddContext(l,consts.ValidatorPrefix,context) if extraAddBindDataContext == nil { response.ErrorSystem(context, "category 表单验证器json化失败", "") } else { //验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性 (&category.Category{}).List(extraAddBindDataContext) } }5. 注册验证器在/app/http/validator/common/register_validator/web_register_validator.go中注册验证器
引用包为goskeleton/app/http/validator/web/category
// 「测试注册」任务分类管理 { key = consts.ValidatorPrefix + "CategoryList" containers.Set(key,category.List{}) }6. 注册路由器在/routers/web.go中「后端接口路由」中添加「获取分类列表的路由」
// 「我的测试」获取分类列表 backend.GET("category", validatorFactory.Create(consts.ValidatorPrefix+"CategoryList") )
二、验证 1. 启动项目启动cmd/web/main.go
2. 测试在浏览器测试下述网站
http://127.0.0.1:20201/admin/category?page=1&limit=3 http://127.0.0.1:20201/admin/category?page=1&limit=6 http://127.0.0.1:20201/admin/category?page=2&limit=3 http://127.0.0.1:20201/admin/category?page=2&limit=10测试效果如下
参考感谢GinSkeleton作者张奇峰提供的框架。张馆长建了一个QQ群273078549,答疑很及时,十分感谢。
[1] https://gitee.com/daitougege/GinSkeleton
[2] https://gitee.com/daitougege/gin-skeleton-admin-backend



