当前位置:网站首页>Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
2022-07-01 15:38:00 【剑客阿良_ALiang】
目录
学习笔记,写到哪是哪。
接着上一篇文章:Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)_剑客阿良_ALiang的博客-CSDN博客
数据库初始化配置已经弄好了,上一篇文章中简单的写了一个demo接口。
本篇文章接着上一篇,对表进行增删改查,同时对时间字段进行特殊处理。
项目地址:github地址
新增修改接口
一般我们在实际项目中,会把新增接口和更新修改接口合并,也就是传递参数中带ID的话则为修改,不带ID的话为新增。
按照项目结构的编写方式,在student_service下新增AddOrUpdateStudent方法。
方法代码如下:
//新增或者更新学生
func (t StudentImpl) AddOrUpdateStudent(student *db_entity.Student) rsp.ResponseMsg {
log.Logger.Info("新增或者更新学生参数:", log.Any("Student", student))
_db := mysql.GetDB()
if student.Id != 0 {
_db.Model(&student).Updates(student)
} else {
_db.Create(&student)
}
return *rsp.SuccessMsg("操作成功")
}注意:在接口测试中你会发现传递update_time时间字段解析错误,在下面的小结里面会讲到自定义事件结构体来解决json时间字段序列化问题。
查询所有未逻辑删除接口
在student_service下新增SelectAll方法,对del_flag为0的用户进行查询。
方法代码如下:
//查询所有学生
func (t StudentImpl) SelectAll() rsp.ResponseMsg {
log.Logger.Info("查询所有学生")
_db := mysql.GetDB()
var _result []db_entity.Student
_db.Where("del_flag = ?", 0).Find(&_result)
return *rsp.SuccessMsg(_result)
}根据ID删除接口
在student_service下新增根据ID删除数据接口。
方法代码如下:
//根据id删除学生
func (t StudentImpl) DeleteById(id int32) rsp.ResponseMsg {
log.Logger.Info("根据id删除学生")
_db := mysql.GetDB()
_db.Delete(&db_entity.Student{}, id)
return *rsp.SuccessMsg("删除成功")
}时间字段序列化问题处理
由于json不能直接序列化time.Time类型,网上有多种解决方法,我这边主要采用自定义时间类型重写序列化方法的方法来解决。
在constants目录下,创建common.go文件。
common.go代码如下:
package constants
import (
"database/sql/driver"
"fmt"
"time"
)
type HTime struct {
time.Time
}
var (
formatTime = "2006-01-02 15:04:05"
)
func (t HTime) MarshalJSON() ([]byte, error) {
formatted := fmt.Sprintf("\"%s\"", t.Format(formatTime))
return []byte(formatted), nil
}
func (t *HTime) UnmarshalJSON(data []byte) (err error) {
now, err := time.ParseInLocation(`"`+formatTime+`"`, string(data), time.Local)
*t = HTime{Time: now}
return
}
func (t HTime) Value() (driver.Value, error) {
var zeroTime time.Time
if t.Time.UnixNano() == zeroTime.UnixNano() {
return nil, nil
}
return t.Time, nil
}
func (t *HTime) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*t = HTime{Time: value}
return nil
}
return fmt.Errorf("can not convert %v to timestamp", v)
}
注意:
1、重新定义HTime结构体,对序列化方法进行调整。
2、Student结构体中UpdateTime字段数据类型调整为HTime,调整后如下。
type Student struct {
Id int32 `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT;comment:'id'"`
Name string `json:"name" gorm:"column:name;type:varchar(255);comment:'名字'"`
Age int64 `json:"age" gorm:"column:age;comment:'年龄'"`
Content string `json:"content" gorm:"column:content;type:varchar(255);comment:'描述'"`
UpdateTime constants.HTime `json:"update_time" time_format:"unix" gorm:"column:update_time"`
DelFlag int64 `json:"del_flag" gorm:"column:del_flag;comment:'删除标识'"`
}接口验证
controller层增加接口代码,如下:
//新增或者修改一个学生信息
func (s StudentController) AddOrUpdateStudent(context *gin.Context) {
var addOrUpdateStudent db_entity.Student
log.Logger.Info("AddOrUpdateStudent接口")
if err := context.ShouldBindJSON(&addOrUpdateStudent); err != nil {
log.Logger.Panic("参数异常")
}
if _, err := json.Marshal(addOrUpdateStudent); err != nil {
log.Logger.Panic("参数解析异常")
}
_rsp := services.StudentServ.AddOrUpdateStudent(&addOrUpdateStudent)
context.JSON(http.StatusOK, _rsp)
}
//查询所有学生
func (s StudentController) SelectAll(context *gin.Context) {
log.Logger.Info("SelectAll接口")
_rsp := services.StudentServ.SelectAll()
context.JSON(http.StatusOK, _rsp)
}
//根据id删除学生
func (s StudentController) DeleteById(context *gin.Context) {
log.Logger.Info("DeleteById接口")
_id := context.Query("id")
_a, _ := strconv.ParseInt(_id, 10, 64)
_rsp := services.StudentServ.DeleteById(int32(_a))
context.JSON(http.StatusOK, _rsp)
}router增加路由路径,如下:
func StudentRouter(r *gin.Engine) {
r.POST("/student/addOne", controllers.StudentCtrl.StudentAddOne)
r.POST("/student/addOrUpdateStudent", controllers.StudentCtrl.AddOrUpdateStudent)
r.GET("/student/selectAll", controllers.StudentCtrl.SelectAll)
r.DELETE("/student/deleteById", controllers.StudentCtrl.DeleteById)
}
分别验证一下3个接口。
新增修改接口

![]()
查询接口

删除接口


小结
最近说实话有点忙了,在忙也要抽时间学习啊。后面继续操作一些复杂的查询操作。
参考链接:gorm time.Time 使用钩子函数解决反序列化问题_qq_26372385的博客-CSDN博客

边栏推荐
- Day 3 of rhcsa study
- Sort out the four commonly used sorting functions in SQL
- 《QT+PCL第六章》点云配准icp系列5
- Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
- 如何实现时钟信号分频?
- Guide de conception matérielle du microcontrôleur s32k1xx
- TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (III)
- Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
- Intelligent operation and maintenance practice: banking business process and single transaction tracking
猜你喜欢

S32K1xx 微控制器的硬件设计指南

Implementation of wechat web page subscription message

Guide de conception matérielle du microcontrôleur s32k1xx

C#/VB.NET 合并PDF文档

Basic use process of cmake
Implementation of deploying redis sentry in k8s

张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例

【目标跟踪】|STARK
Sort out the four commonly used sorting functions in SQL

有些能力,是工作中学不来的,看看这篇超过90%同行
随机推荐
Survey of intrusion detection systems:techniques, datasets and challenges
常见健身器材EN ISO 20957认证标准有哪些
MySQL高级篇4
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
Intelligent operation and maintenance practice: banking business process and single transaction tracking
How to realize clock signal frequency division?
采集数据工具推荐,以及采集数据列表详细图解流程
Photoshop plug-in HDR (II) - script development PS plug-in
C#/VB.NET 合并PDF文档
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
What are the test items of juicer ul982
远程办公经验?来一场自问自答的介绍吧~ | 社区征文
Implementation of wechat web page subscription message
【一天学awk】函数与自定义函数
Tableapi & SQL and MySQL grouping statistics of Flink
她就是那个「别人家的HR」|ONES 人物
Redis seckill demo
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
Redis high availability principle
Hardware design guide for s32k1xx microcontroller