MICUU
心情
所有
图集
登录
搜索
原创
gin05-gorm读取数据库返回查询结果到HTML
米醋儿
发布于:2022-08-18
   go语言有一点逻辑还是合PHP的有一点不一样的,特别是gorm包一些简单的逻辑,初次使用的时候可能会需要自己弯弯绕绕想明白理解一下。下面是一个简单的示例 main.go ``` package main //引用包文件 import ( "gin/app/model" "github.com/gin-gonic/gin" "net/http" ) type Content struct { //gorm.Model Id int `gorm:"type:int;not null",json:"id"` Title string `gorm:"type:varchar(200);not null",json:"title"` Description string `gorm:"type:varchar(1000);not null",json:"description"` Keywords string `gorm:"type:varchar(200);not null",json:"keywords"` Thumbnail string `gorm:"type:varchar(200);not null",json:"thumbnail"` ModelId int `gorm:"type:int(11);",json:"model_id"` TitleStyle string `gorm:"type:varchar(100);",json:"title_style"` CateId int `gorm:"type:int(10);",json:"cate_id"` RelHits int `gorm:"type:int(10);",json:"rel_hits"` Hits int `gorm:"type:int(10);",json:"hits"` Ordid int `gorm:"type:int(10);",json:"ordid"` AddTime int `gorm:"type:int(12);",json:"add_time"` LastTime int `gorm:"type:int(12);",json:"last_time"` Status int `gorm:"type:int(12);",json:"status"` Top int `gorm:"type:int(12);",json:"top"` HiddenInfo string `gorm:"type:varchar(1000);",json:"hidden_info"` Score string `gorm:"type:varchar(1000);",json:"score"` Recommend int `gorm:"type:int(10);",json:"recommend"` Comment string `gorm:"type:varchar(1000);",json:"comment"` Uid int `gorm:"type:int(10);",json:"uid"` Like int `gorm:"type:int(10);",json:"like"` Unlike int `gorm:"type:int(10);",json:"unlike"` Index int `gorm:"type:int(10);",json:"index"` Baijiahao int `gorm:"type:int(10);",json:"baijiahao"` IsMd int `gorm:"type:int(10);",json:"is_md"` From string `gorm:"type:varchar(100);",json:"from"` Fav int `gorm:"type:int(10);",json:"fav"` Imgs string `gorm:"type:json;",json:"imgs"` Pid int `gorm:"type:int(10);",json:"pid"` AllowComment int `gorm:"type:int(10);",json:"allow_comment"` //DeleteTime int `gorm:"column:delete_at",json:"delete_at"` // 将列名设为 //DeleteTime gorm.DeletedAt `gorm:"type:int(11);",json:"delete_time"` //DeletedTime *time.Time `sql:"index" json:"delete_time"` } func (Content) TableName() string { //config ,err := ini.Load("config/db.ini") //if err != nil { // fmt.Println("配置文件读取错误,请检查文件路径:", err) //} // //prefix := config.Section("database").Key("DbPrefix").MustString("zhe_") //绑定MYSQL表名为users //return prefix+"content" return "zhe_content" } func main() { router := gin.Default() router.LoadHTMLGlob("templates/**/*") //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") router.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index/index.html", gin.H{ "title": "Main website", }) }) router.GET("/test/:id", model.ContentGetArticle) // 此 handler 将匹配 /user/john 但不会匹配 /user/ 或者 /user router.GET("/read/:id", func(c *gin.Context) { id := c.Param("id") c.HTML(http.StatusOK, "article/article.html", gin.H{ "id": id, }) }) router.Run(":8001") } ``` Content.go ``` package model import ( "fmt" "github.com/gin-gonic/gin" "gorm.io/driver/mysql" "gorm.io/gorm" ) type Content struct { //gorm.Model Id int `gorm:"type:int;not null",json:"id"` Title string `gorm:"type:varchar(200);not null",json:"title"` Description string `gorm:"type:varchar(1000);not null",json:"description"` Keywords string `gorm:"type:varchar(200);not null",json:"keywords"` Thumbnail string `gorm:"type:varchar(200);not null",json:"thumbnail"` ModelId int `gorm:"type:int(11);",json:"model_id"` TitleStyle string `gorm:"type:varchar(100);",json:"title_style"` CateId int `gorm:"type:int(10);",json:"cate_id"` RelHits int `gorm:"type:int(10);",json:"rel_hits"` Hits int `gorm:"type:int(10);",json:"hits"` Ordid int `gorm:"type:int(10);",json:"ordid"` AddTime int `gorm:"type:int(12);",json:"add_time"` LastTime int `gorm:"type:int(12);",json:"last_time"` Status int `gorm:"type:int(12);",json:"status"` Top int `gorm:"type:int(12);",json:"top"` HiddenInfo string `gorm:"type:varchar(1000);",json:"hidden_info"` Score string `gorm:"type:varchar(1000);",json:"score"` Recommend int `gorm:"type:int(10);",json:"recommend"` Comment string `gorm:"type:varchar(1000);",json:"comment"` Uid int `gorm:"type:int(10);",json:"uid"` Like int `gorm:"type:int(10);",json:"like"` Unlike int `gorm:"type:int(10);",json:"unlike"` Index int `gorm:"type:int(10);",json:"index"` Baijiahao int `gorm:"type:int(10);",json:"baijiahao"` IsMd int `gorm:"type:int(10);",json:"is_md"` From string `gorm:"type:varchar(100);",json:"from"` Fav int `gorm:"type:int(10);",json:"fav"` Imgs string `gorm:"type:json;",json:"imgs"` Pid int `gorm:"type:int(10);",json:"pid"` AllowComment int `gorm:"type:int(10);",json:"allow_comment"` //DeleteTime int `gorm:"column:delete_at",json:"delete_at"` // 将列名设为 //DeleteTime gorm.DeletedAt `gorm:"type:int(11);",json:"delete_time"` //DeletedTime *time.Time `sql:"index" json:"delete_time"` } func (Content) TableName() string { //config ,err := ini.Load("config/db.ini") //if err != nil { // fmt.Println("配置文件读取错误,请检查文件路径:", err) //} // //prefix := config.Section("database").Key("DbPrefix").MustString("zhe_") //绑定MYSQL表名为users //return prefix+"content" return "zhe_content" } func ContentGetArticle(c *gin.Context) { dsn := "root:123456@tcp(127.0.0.1:3306)/micuer20220812?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { fmt.Println("链接数据库错误", err) } id := c.Param("id") name := c.Query("name") content := Content{} db.Debug().Where("id = ?",id).Find(&content) fmt.Println("查询") fmt.Println(content) fmt.Println(id) fmt.Println("res数据") //c.JSON(200,gin.H{ // "res":content, // "name":name, //}) c.HTML(200,"article/article.html",gin.H{ "res":content, "name":name, }) //c.HTML(200,"article/article.html", gin.H{ // "res": content, //}) } ``` article.html ``` {{ define "article/article.html" }} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{.id}} - 文章详情</title> </head> <body> <h1>现在是文章详情,为啥显示成了json</h1> {{.id}} <h2> {{.res.Id}} - {{.res.Title}} </h2> <h2> name {{.name}} </h2> </body> </html> {{ end }} ```
注:原创不易,转载请注明出处(
https://micuu.com/new/3011.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-08-18
上一篇:
idea文件对比
下一篇:
专题文章详情页面简单进行优化,目前支持查看专题全部文章链接,样式的话后面再进行优化吧
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>