欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 使用GIN实现操作数据库简单的web项目

使用GIN实现操作数据库简单的web项目

2025/6/26 23:10:19 来源:https://blog.csdn.net/qq_33371766/article/details/144468755  浏览:    关键词:使用GIN实现操作数据库简单的web项目

1、gin框架的介绍

Gin是一个用Go(Golang)编写的HTTP Web框架,它是一个轻量级、高性能的框架,广泛用于构建RESTful API、Web服务和单页应用程序。以下是Gin的一些主要特点和优势:

特点:

  1. 性能:Gin是一个非常快速的框架,它是基于HTTP路由器(httprouter)构建的,这个路由器以其高性能而闻名。

  2. 中间件支持:Gin允许使用中间件来处理HTTP请求,例如身份验证、日志记录、CORS处理等。

  3. 路由:Gin提供了强大的路由能力,支持HTTP的GET、POST、PUT、DELETE、PATCH等方法,并允许参数和路径的绑定。

  4. 错误管理:Gin有内置的错误处理机制,可以很容易地管理HTTP错误。

  5. 模型绑定和验证:Gin支持将请求体绑定到结构体,并可以进行数据验证。

  6. 渲染:Gin支持多种类型的渲染,如JSON、XML、HTML等。

  7. 扩展性:Gin易于扩展,你可以根据需要添加自己的中间件。

安装:

安装Gin非常简单,使用Go的包管理工具go get即可:

go get -u github.com/gin-gonic/gin

2、使用goland编译器开发

配置这三个配置即可,如果安装好go环境之后,前面两个默认就会配置好。第三个配置如图配置,下载go的一些依赖的代理地址。

3、代码

3.1、目录结构介绍

说明:

migrate:存放数据库的链接方法

model:数据库的对象

service:操作数据库的方法

static:里面存放的是css、js、图片等

templates:静态界面html

go.mod:项目的依赖

main.go:项目的启动文件

3.2、代码实现

mysql_db.go

package migrateimport ("first/model""gorm.io/gorm"
)
import "gorm.io/driver/mysql"var DB *gorm.DB// 在 Go 语言中,如果函数、变量或类型以小写字母开头,则它们在包外是不可见的,
// 这被称为 “unexported” 或 “private”。这意味着您不能从其他包中直接调用这些未导出的标识符。
func InitDB() *gorm.DB {// DSN (Data Source Name) 格式: "用户名:密码@tcp(地址:端口)/数据库?charset=utf8mb4&parseTime=True&loc=Local"dsn := "root:123456@tcp(127.0.0.1:3307)/go?charset=utf8mb4&parseTime=True&loc=Local"var err errorDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})if err != nil {var x any = "failed to connect database"panic(x)}// 自动迁移模式DB.AutoMigrate(&model.User{})return DB
}

  在 Go 语言中,如果函数、变量或类型以小写字母开头,则它们在包外是不可见的,  这被称为 “unexported” 或 “private”。这意味着您不能从其他包中直接调用这些未导出的标识符。

  外部需要调用该方法名需要大写开头

user.go

定义表字段,数据库会默认带id和创建时间、更新时间、删除时间四个字段

package modelimport "gorm.io/gorm"type User struct {gorm.ModelUsername string `gorm:"size:100"`Password stringEmail string `gorm:"size:100;uniqueIndex"`
}func Migrate(db *gorm.DB) error {return db.AutoMigrate(&User{})}

user_service.go

package serviceimport ("first/migrate""first/model""fmt""github.com/gin-gonic/gin""gorm.io/gorm"
)func GetUsers(c *gin.Context) {var users []model.Usermigrate.DB.Find(&users)c.JSON(200, users)
}func CreateUser(c *gin.Context) {var user model.Userif err := c.ShouldBindJSON(&user); err != nil {c.JSON(400, gin.H{"error": err.Error()})return}migrate.DB.Create(&user)c.JSON(200, user)
}// 根据用户名查询用户
func GetUserByUsername(username string) (*model.User, error) {var user model.Userresult := migrate.DB.Where("username = ?", username).First(&user)if result.Error != nil {if result.Error == gorm.ErrRecordNotFound {fmt.Println("User not found")} else {fmt.Println(result.Error)}}// 输出查询结果fmt.Printf("User: %+v\n", user)return &user, nil
}

main.go

package mainimport ("encoding/json""first/migrate""first/service""github.com/gin-gonic/gin""github.com/thinkerou/favicon""gorm.io/gorm""log""net/http"
)// 自定义中间件 拦截器
func myHandler() (gin.HandlerFunc) {return func(context *gin.Context) {context.Set("user_session", "userid-66666666666666")// 放行context.Next()// 阻断// context.Abort()}
}var db *gorm.DBfunc main() {ginServer := gin.Default()// 注册中间件ginServer.Use(myHandler())ginServer.LoadHTMLGlob("./templates/*")ginServer.Static("/static", "./static")ginServer.Use(favicon.New("./static/img/favicon.png"))// http://localhost:8082/helloginServer.GET("/hello", func(context *gin.Context) {user_session := context.MustGet("user_session").(string)log.Println("获取的session:", user_session)context.JSON(200, gin.H{"code": 200,"msg": "hello world!",})})// http://localhost:8082/indexginServer.GET("/index", func(context *gin.Context) {context.HTML(200, "index.html", gin.H{"msg": "后端的数据,你好,go!",})})// http://localhost:8082/user/info?userid=1001&username=frankginServer.GET("/user/info", func(context *gin.Context) {user_session := context.MustGet("user_session").(string)log.Println("获取的session:", user_session)userid := context.Query("userid")username := context.Query("username")context.JSON(200, gin.H{"userid": userid,"username": username,"user_session": user_session,})})// http://localhost:8082/user/1001/frankginServer.GET("/user/:userid/:username", func(context *gin.Context) {userid := context.Param("userid")username := context.Param("username")context.JSON(200, gin.H{"userid": userid,"username": username,})})ginServer.POST("/json", func(context *gin.Context) {data, _ := context.GetRawData()var m map[string]interface{}_ = json.Unmarshal(data, &m)context.JSON(http.StatusOK, m)})// 表单保存ginServer.POST("/user", func(context *gin.Context) {username := context.PostForm("username")password := context.PostForm("password")context.JSON(200, gin.H{"password": password,"username": username,})})ginServer.PUT("/user", func(context *gin.Context) {context.JSON(200, gin.H{"msg": "修改的界面",})})ginServer.DELETE("/user", func(context *gin.Context) {context.JSON(200, gin.H{"msg": "删除的界面",})})// 重定向ginServer.GET("/test", func(context *gin.Context) {context.Redirect(http.StatusMovedPermanently, "https://www.baidu.com")})// 404ginServer.NoRoute(func(context *gin.Context) {context.HTML(http.StatusNotFound, "404.html", nil)})// 路由组userGroup := ginServer.Group("/user"){userGroup.GET("/add")userGroup.GET("/login")userGroup.GET("/logout")}orderGroup := ginServer.Group("/order"){orderGroup.GET("/add")orderGroup.GET("/delete")orderGroup.GET("/update")}// 操作数据库db = migrate.InitDB()// 获取所有用户ginServer.GET("/users", service.GetUsers)// 保存用户ginServer.POST("/user/create", service.CreateUser)// http://127.0.0.1:8082/users/frank2 根据名称查询用户信息ginServer.GET("/users/:username", func(context *gin.Context) {username := context.Param("username")user, _ := service.GetUserByUsername(username)context.JSON(200, gin.H{"data": user,})})ginServer.Run(":8082")
}

完整的代码地址:

gin-study: 学习gin框架实现的第一个web项目

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com