Gin 实现基础 CRUD 接口
前面2篇讲了关于 gin + mysql + jwt + rbac 等基础 web搭建操作, 主要目的还是学习 go 语言的一些应用工具,
然后本篇继续来实现一个名为 notice
的公告模块, 包含数据的增删查改, 这个几乎是咱们讨饭的重点, 当然接口逻辑都是比较简单, 体验一下就好.
项目目录
我是在 mac 终端下, 用命令行来模拟一下 tree
的功能, 体现目录结构.
cj@mini gin-shop-admin %
find . -path './.git' -prune -o -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
.
|____go.mod
|____internal
| |____middleware
| | |____auth.go
| |____db
| | |____db.go
|____go.sum
|____api
| |____routers.go
| |____handlers
| | |____auth
| | | |____views.go
| | |____user
| | | |____menu.go
| | | |____views.go
| | |____notice
| | | |____views.go
| | |____rule
| | | |____views.go
|____.vscode
| |____extensions.json
|____tmp
| |____runner-build
|____main.go
|____pkg
| |____utils
| | |____response.go
| | |____tools.go
| |____jwtt
| | |____jwt.go
cj@mini gin-shop-admin %
数据库表
主要是 notice
表, 字段如下:
-- ###### 公告模块
drop table if exists notice;
create table notice (
id int not null auto_increment primary key comment '自增id'
, title varchar(200) not null comment '公告标题'
, content text not null comment '公告内容'
, orders int not null default 50
, create_time datetime default current_timestamp
, update_time datetime default current_timestamp on update current_timestamp
);
模块路由
api/routers.go
package api
import (
"github.com/gin-gonic/gin"
"youge.com/api/handlers/auth"
"youge.com/api/handlers/notice"
"youge.com/internal/middleware"
)
// 统一注册入口
func RegisterAllRouters(r *gin.Engine) {
// 登录认证模块
authGroup := r.Group("/api/auth")
{
// auth.POST("/register", Register)
authGroup.POST("/login", auth.Login)
}
// 用户管理模块
// 公告模块
noticeGroup := r.Group("api/notice")
noticeGroup.Use(middleware.JWT()) // 也是需要 token 认证
{
noticeGroup.GET("/notice", notice.GetNoticeList) // get
noticeGroup.POST("/notice", notice.CreateNotice) // add
noticeGroup.PUT("/notice/:id", notice.UpdateNotice) // update
noticeGroup.DELETE("/notice/:id/delete", notice.DeleteNotice) // del
}
// ... 更多模块
}
对应的接口逻辑实现都放在 /api/handlers/notice/views.go
里面, 不做分层了哈, 直接干.
GET - 查询公告接口
前端: http://localhost:8000/api/notice/notice?page=1&limit=10
后端: /api/handlers/notice/views.go
=> GetNoticeList
- 分页查询, 小数据版的 limit, offset 实现
- 请求参数验证 + 默认值实现
package notice
import (
"strconv"
"time"
"github.com/gin-gonic/gin"
"youge.com/internal/db"
"youge.com/pkg/utils"
)
// 公告表
type Notice struct {
ID int `db:"id" json:"id"`
Title string `db:"title" json:"title"`
Content string `db:"content" json:"content"`
Orders string `db:"orders" json:"orders"`
CreatedAt time.Time `db:"create_time" json:"create_time"`
UpdatedAt time.Time `db:"update_time" json:"update_time"`
}
// 请求参数处理, omitempty, 表示可选
type NoticeRequest struct {
Page int `form:"page" binding:"omitempty,min=1"`
Limit int `form:"limit" binding:"omitempty,min=1,max=1000"`
}
// 接口: 获取通知列表
func GetNoticeList(c *gin.Context) {
// 绑定并验证参数
var req NoticeRequest
if err := c.ShouldBindQuery(&req); err != nil {
utils.BadRequest(c, "请求参数错误")
return
}
// 设置默认值, page=1, limit=10
if req.Page == 0 {
req.Page = 1
}
if req.Limit == 0 {
req.Limit = 10
}
// 分页查询数据
// 页面偏移量
offset := (req.Page - 1) * req.Limit
mysql := `
select
id
, title
, content
, orders
, create_time
, update_time
from notice
order by create_time desc
limit ?, ?;
`
var noticeList []Notice
if err := db.Query(¬iceList, mysql, offset, req.Limit); err != nil {
utils.BadRequest(c, "获取数据失败")
return
}
// 查询总条数
var total int
// 这个sql 肯定不会错, 除非表不在, 不校验了
db.Get(&total, "select count(*) as total from notice;")
// 返回结果
utils.Success(c, gin.H{
"list": noticeList,
"totalCount": total,
"limit": req.Limit,
})
}
补充参数验证的工具函数
主要为了对标一下 fastapi
这个我觉得超级棒的 web 框架, 现在放弃的原因主要是, go 可能性能更为主流.
这里安装一下第三方库, 它和 Python 的 pydantic
差不多呢.
go get "github.com/go-playground/validator/v10"
工具函数放在了 pkg/utils/tools.go
中了.
package utils
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
// 验证和绑定前端 json 请求体
func BindAndValidate(c *gin.Context, request interface{}) error {
// 1. 绑定请求体到结构体
if err := c.ShouldBindJSON(request); err != nil {
return err
}
// 2. 验证结构体字段
validate := validator.New()
if err := validate.Struct(request); err != nil {
return err
}
return nil
}
POST - 新增公告接口
前端: http://localhost:8000/api/notice/notice
前端请求体 json : {"title": "test01", "content": "test123"}
后端: /api/handlers/notice/views.go
=> CreateNotice
func CreateNotice(c *gin.Context) {
// 验证前端传过来的 json 参数
var req struct {
Title string `json:"title" validate:"required,min=1,max=200"`
Content string `json:"content" validate:"required"`
}
if err := utils.BindAndValidate(c, &req); err != nil {
utils.BadRequest(c, "请求参数错误")
return
}
// 数据写入数据库
mysql := `
insert into notice(title, content)
values (?, ?);
`
rows, err := db.Exec(mysql, req.Title, req.Content)
if err != nil {
utils.BadRequest(c, "新增公告失败")
return
}
utils.Success(c, gin.H{
"affectedRows": rows,
})
}
PUT - 修改公告接口
前端 : http://localhost:8000/api/notice/notice/9
后端: /api/handlers/notice/views.go
=> UpdateNotice
func UpdateNotice(c *gin.Context) {
// 获取并校验路径参数
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
utils.BadRequest(c, "请求参数错误")
return
}
// 根据 id 修改数据库内容
// 直接通过 var 定义的结构体是匿名的, 用于局部, 而 type 是整个包内
var req struct {
Title string `json:"title" validate:"required,min=1,max=200"`
Content string `json:"content" validate:"required"`
}
if err := utils.BindAndValidate(c, &req); err != nil {
utils.BadRequest(c, "请求参数错误")
return
}
mysql := `
update notice
set title = ?, content = ?
where id = ?;
`
rows, err := db.Exec(mysql, req.Title, req.Content, id)
// id 不存在 或者 更新异常都提示失败
if err != nil || rows == 0 {
utils.BadRequest(c, "更新数据错误")
return
}
utils.Success(c, gin.H{
"affectedRows": rows,
})
}
DELETE - 删除公告接口
前端 : http://localhost:8000/api/notice/notice/9/delete
后端: /api/handlers/notice/views.go
=> DeleteNotice
func DeleteNotice(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
utils.BadRequest(c, "请求参数错误")
return
}
// 根据 id 进行删除, 错误就不处理得了, 不会错的
rows, _ := db.Exec("delete from notice where id = ?", id)
utils.Success(c, gin.H{
"affectedRows": rows,
})
}
至此, 一个常用的 CRUD 接口就写完了, 其他模块都是大同小异的, 只是复杂度不同而已.
Gin 实现基础 CRUD 接口的更多相关文章
- GO学习-(14) Go语言基础之接口
Go语言基础之接口 接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口 接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类 ...
- 速战速决 (4) - PHP: 类基础, 抽象类, 接口, trait
[源码下载] 速战速决 (4) - PHP: 类基础, 抽象类, 接口, trait 作者:webabcd 介绍速战速决 之 PHP 类基础 抽象类 接口 trait 示例1.类的相关知识点 1(基础 ...
- [.net 面向对象编程基础] (16) 接口
[.net 面向对象编程基础] (16) 接口 关于“接口”一词,跟我们平常看到的电脑的硬件“接口”意义上是差不多的.拿一台电脑来说,我们从外面,可以看到他的USB接口,COM接口等,那么这些接口的目 ...
- spring中基础核心接口总结
spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...
- Go语言基础之接口
Go语言基础之接口 接口(interface)定义了一个对象的行为规范,只定义规范不实现,由具体的对象来实现规范的细节. 接口 接口介绍 在Go语言中接口(interface)是一种类型,一种抽象的类 ...
- C#基础--类/接口/成员修饰符,多态、重载、重写,静态和非静态
C#基础--类/接口/成员修饰符,多态.重载.重写,静态和非静态 类/接口/成员修饰符 C#修饰符---接口: 接口默认访问符是internal接口的成员默认访问修饰符是public C#修饰符--类 ...
- Java基础十--接口
Java基础十--接口 一.接口的定义和实例 /* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 8 ...
- Java基础-面向接口(interface)编程
Java基础-面向接口(interface)编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的“类 ...
- MyBatis-Plus使用(2)-CRUD接口
参考文档:https://mybatis.plus/guide/crud-interface.html MyBatis-Plus自带的CRUD方法分为Mapper层和Service层,大多数功能是重叠 ...
- mybatispluys-Mapper CRUD 接口
Mapper CRUD 接口 通用 CRUD 封装BaseMapper (opens new window)接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部 ...
随机推荐
- Python 潮流周刊#89:Python 3.14 的新型解释器!(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- script crossorigin 属性
来源:https://juejin.cn/post/6969825311361859598 <script src="xxxx" crossorigin="anon ...
- .netCore 使用 Quartz 实例
一.参考源文链接 1.https://www.likecs.com/show-897836.html 2.https://blog.csdn.net/weixin_43614067/article/d ...
- LayerSkip: 使用自推测解码加速大模型推理
自推测解码是一种新颖的文本生成方法,它结合了推测解码 (Speculative Decoding) 的优势和大语言模型 (LLM) 的提前退出 (Early Exit) 机制.该方法出自论文 Laye ...
- 实现Windows之间(win10)的桌面连接的三步走方法
实现Windows之间(win10)的远程桌面连接的三步走方法 目录 目录 实现Windows之间(win10)的远程桌面连接的三步走方法 目录 环境 step1:打开两台Windows电脑的 ...
- Mac下打开进入/usr/local等隐藏目录
教程 Mac下/usr/local目录默认是对于Finder是隐藏,如果需要到/usr/local下去,打开Finder,然后使用command+shift+G,在弹出的目录中填写/usr/local ...
- markdown设置目录、锚点
目录 在编辑时正确使用标题,在段首输入[toc]即可 锚点 创建到命名锚记的链接的过程分为两步: 首先是建立一个跳转的连接: [说明文字](#jump) 然后标记要跳转到什么位置,注意id要与之前(# ...
- matplotlib -- 绘图操作 -- 数据分析三剑客
博客地址:https://www.cnblogs.com/zylyehuo/ 开发环境 anaconda 集成环境:集成好了数据分析和机器学习中所需要的全部环境 安装目录不可以有中文和特殊符号 jup ...
- 关于oracle pfile和spfile文件说明
•Pfile(Parameter File,参数文件):是基于文本格式的参数文件,含有数据库的配置参数. 默认的名称为"init+例程名.ora",这是一个文本文件,可以用任何文本 ...
- Oracle配置和性能优化方法
性能是衡量软件系统的一个重要部分,可能引起性能低下的原因很多,如CPU/内存/网络资源不足,硬盘读写速度慢,数据库配置不合理,数据库对象规划或存储方式不合理,模块设计对性能考虑不足等. 1 ...