当前位置:网站首页>Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
Go语学习笔记 - gorm使用 - 数据库配置、表新增 | Web框架Gin(七)
2022-06-30 20:09:00 【游戏编程】
目录
学习笔记,写到哪是哪。
接着上一篇文章:Go语学习笔记 - 增加文件下载工具 | Web框架Gin(六)_剑客阿良_ALiang的博客-CSDN博客
我已经把暂时想到的工具类补充好了,开始数据库的连接和使用。
从这篇开始会慢慢在增加数据库的相关操作。
项目地址:github地址
依赖安装
首先安装一下所需要的gorm和mysql驱动。
安装指令如下:
go get -u gorm.io/gorm
go get -u gorm.io/driver/mysql
Mmysql配置
修改配置文件toml_config.go,增加mysql相关配置。
具体的使用方式参考:Go语学习笔记 - 配置文件使用、日志配置 | Web框架Gin(二)_剑客阿良_ALiang的博客-CSDN博客_config.toml
toml_config.go文件调整后代码如下:
package tomlimport ( "fmt" "github.com/spf13/viper")type TomlConfig struct { AppName string Log LogConfig Mysql MysqlConfig}// 日志保存地址type LogConfig struct { Path string Level string}// 日志保存地址type MysqlConfig struct { Host string User string Password string DbName string Port int64}var c TomlConfigfunc init() { // 设置文件名 viper.SetConfigName("config") // 设置文件类型 viper.SetConfigType("toml") // 设置文件路径,可以多个viper会根据设置顺序依次查找 viper.AddConfigPath(".") viper.AutomaticEnv() err := viper.ReadInConfig() if err != nil { panic(fmt.Errorf("fatal error config file: %s", err)) } viper.Unmarshal(&c)}func GetConfig() TomlConfig { return c}
可以看到增加了MysqlConfig结构体。
配置文件config.toml文件增加mysql对应配置,修改后如下:
appName = "learn-gin"[log]level = "debug"path = "logs/learn.log"[mysql]host = "xx.xx.xx.xx"user = "root"password = "xxxxxx"dbName = "test-c"port = 3306
Mysql初始化
增加数据库连接初始化代码,mysql_init.go文件添加至config/mysql下,如下图。

mysql_init.go文件代码如下:
package mysqlimport ( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "learn-gin/config/toml")var _db *gorm.DBfunc init() { username := toml.GetConfig().Mysql.User //账号 password := toml.GetConfig().Mysql.Password //密码 host := toml.GetConfig().Mysql.Host //数据库地址,可以是Ip或者域名 port := toml.GetConfig().Mysql.Port //数据库端口 Dbname := toml.GetConfig().Mysql.DbName //数据库名 timeout := "10s" //连接超时,10秒 //拼接下dsn参数, dsn格式可以参考上面的语法,这里使用Sprintf动态拼接dsn参数,因为一般数据库连接参数,我们都是保存在配置文件里面,需要从配置文件加载参数,然后拼接dsn。 dsn := fmt.Sprintf("%s:%[email protected](%s:%d)/%s?charset=utf8&parseTime=True&loc=Local&timeout=%s", username, password, host, port, Dbname, timeout) var err error //连接MYSQL, 获得DB类型实例,用于后面的数据库读写操作。 _db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{ Logger: logger.Default.LogMode(logger.Info), }) if err != nil { panic("连接数据库失败, error=" + err.Error()) } sqlDB, _ := _db.DB() //设置数据库连接池参数 sqlDB.SetMaxOpenConns(100) //设置数据库连接池最大连接数 sqlDB.SetMaxIdleConns(20) //连接池最大允许的空闲连接数,如果没有sql任务需要执行的连接数大于20,超过的连接会被连接池关闭。}func GetDB() *gorm.DB { return _db}
可以通过GetDB方法获取_db进行操作。
添加表
我们在数据库创建一张测试表,当前也可以通过gorm迁移一张表结构体到数据库,我们这里暂时先创建。表(表名:student)结构如下:

在项目文件夹下增加db_entity文件夹存放表实体。如下图:

student_entity.go代码如下:
package db_entityimport "time"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 time.Time `json:"update_time" gorm:"column:update_time"` DelFlag int64 `json:"del_flag" gorm:"column:del_flag;comment:'删除标识'"`}// 自定义表名func (Student) TableName() string { return "student"}
表新增操作
按照之前项目结构优化后的方式添加接口,参考:Go语学习笔记 - 项目规范结构调整 | Web框架Gin(三)_剑客阿良_ALiang的博客-CSDN博客
在app/services目录下增加student._service.go文件,代码如下:
package servicesimport ( "learn-gin/app/pojo/req" "learn-gin/app/pojo/rsp" "learn-gin/config/log" "learn-gin/config/mysql" "learn-gin/db_entity" "time")type StudentService interface { AddStudent(req *req.StudentAddReq)}type StudentImpl struct {}// 添加学生func (t StudentImpl) AddStudent(req *req.StudentAddReq) rsp.ResponseMsg { log.Logger.Info("添加学生参数:", log.Any("StudentReq", req)) _db := mysql.GetDB() var _student db_entity.Student _student.Name = req.Name _student.Age = req.Age _student.Content = req.Content _student.UpdateTime = time.Now() _student.DelFlag = 0 _db.Create(&_student) return *rsp.SuccessMsg("添加成功")}
其中app/pojo/req下增加请求结构体StudentAddReq,在app/pojo/req目录下增加student_request.go文件。

代码如下:
package reqtype StudentAddReq struct { Name string `json:"name"` Age int64 `json:"age"` Content string `json:"content"`}
在app/services/service.go增加student服务对象,代码如下:
package servicesvar ( TestServ = &Test{} StudentServ = &StudentImpl{})
在app/controllers下增加student_controller.go文件。

代码如下:
package controllersimport ( "encoding/json" "github.com/gin-gonic/gin" "learn-gin/app/pojo/req" "learn-gin/app/services" "learn-gin/config/log" "net/http")type StudentController struct {}func (s StudentController) StudentAddOne(context *gin.Context) { var addStudentReq req.StudentAddReq log.Logger.Info("StudentAddOne接口") // 将前端穿过来的json数据绑定存储在这个实体类中,BindJSON()也能使用 if err := context.ShouldBindJSON(&addStudentReq); err != nil { log.Logger.Panic("参数异常") } if _, err := json.Marshal(addStudentReq); err != nil { log.Logger.Panic("参数解析异常") } _rsp := services.StudentServ.AddStudent(&addStudentReq) context.JSON(http.StatusOK, _rsp)}
在app/controllers/controllers.go文件增加student_controller中的控制层对象。
代码如下:
package controllersvar ( TestCtrl = &TestController{} StudentCtrl = &StudentController{})
在app/router目录下增加student_router.go文件。

代码如下:
package routerimport ( "github.com/gin-gonic/gin" "learn-gin/app/controllers")func StudentRouter(r *gin.Engine) { r.POST("/student/addOne", controllers.StudentCtrl.StudentAddOne)}
在app/router/router.go下绑定StudentRouter。
package routerimport "github.com/gin-gonic/gin"func InitRouter(r *gin.Engine) { // 测试路由 TestRouter(r) // 学生路由 StudentRouter(r)}
测试接口
代码完成后,我们测试一下表新增接口。

OK,我们看一下表数据。

没什么问题。
小结
没想到写着写着都有30篇了,gorm还在研究,后面会把表操作都写一写。

作者:剑客阿良_ALiang
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- 请指教在线开户需要什么银行卡?另外想问,现在在线开户安全么?
- 二叉查找树(一) - 概念与C语言实现
- NLP paper lead reading | what about the degradation of text generation model? Simctg tells you the answer
- Lumiprobe染料 NHS 酯丨BDP FL NHS 酯研究
- Exness: the final value of US GDP unexpectedly accelerated to shrink by 1.6%
- Common questions and answering skills of project manager interview
- A complete collection of vulnerability scanning tools. Mom doesn't have to worry that I won't find any more vulnerabilities
- Dynamic style binding --style and class
- Jerry's long press reset [chapter]
- 1、生成对抗网络入门
猜你喜欢
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
浅谈代码语言的魅力
AVL balanced binary tree (I) - concept and C language implementation
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
Halcon知识:盘点一下计量对象【1】
Network planning | [five transport layers and six application layers] knowledge points and examples
AVL平衡二叉树(一) - 概念与C语言实现
昨晚 Spark Summit 重要功能发布全在这里(附超清视频)
哈夫曼樹(一)基本概念與C語言實現
断点续传和下载原理分析
随机推荐
杰理之用测试盒配对软件修改注意点【篇】
Summary of PHP file upload (garbled code, move failure, permission, display picture)
Lvalue reference and lvalue reference
Heartbeat uses NFS to make MySQL highly available based on CRM
Network planning | [five transport layers and six application layers] knowledge points and examples
Common questions and answering skills of project manager interview
Analysis of breakpoint continuation and download principle
杰理之触摸按键识别流程【篇】
凌云出海记 | 一零跃动&华为云:共助非洲普惠金融服务
Huffman tree (I) basic concept and C language implementation
亚马逊在阿拉伯联合酋长国限制LGBTQ相关的搜索和产品销售
[1175. prime number arrangement]
Lumiprobe核酸定量丨QuDye dsDNA BR 检测试剂盒
哈夫曼树(一)基本概念与C语言实现
On inline function
Black apple server system installation tutorial, black apple installation tutorial, teach you how to install black apple in detail [easy to understand]
NLP skill tree learning route - (I) route overview
Jerry's touch key recognition process [chapter]
Heartbeat 与DRBD 配置过程
北京大学ACM Problems 1003:Hangover