structs/post.go 结构体分类使用 在线sql转golang struct 生成 post 结构体保存为post.go 。并将之前各个结构体分别放入 structs 文件夹下统一管理 。
package structstype Post struct { IDint64`db:"id" json:"id"` Bgcolorstring `db:"bgcolor" json:"bgcolor"` Textcolor string `db:"textcolor" json:"textcolor"` Headimgstring `db:"headimg" json:"headimg"` Videosrcstring `db:"videosrc" json:"videosrc"` Imgsstring `db:"imgs" json:"imgs"` Htmlstring `db:"html" json:"html"`}
controllers/comment.go 迁移评价机制
文章插图
package controllerimport ( "time" "github.com/gin-gonic/gin" "wolflong.com/vue_gin/structs" "wolflong.com/vue_gin/variable")func QueryComment(c *gin.Context) { db := variable.DB pid := c.Query("pid") rows, err := db.Query(`select id,uid,text,pid,date,name from comments join users using(uid) where pid = ?`, pid) checkError(err) defer rows.Close() var res []structs.Comment for rows.Next() {var c structs.Commenterr = rows.Scan(&c.ID, &c.UID, &c.Text, &c.Pid, &c.Date, &c.Name)checkError(err)res = append(res, c) } c.JSON(200, res)}func DeleteComment(c *gin.Context) { db := variable.DB cid := c.PostForm("id") res, err := db.Exec("delete from comments where id = ?", cid) checkError(err) n, err := res.RowsAffected() checkError(err) if n == 0 {c.JSON(501, gin.H{"message": "failure",})c.Abort()return } c.JSON(200, gin.H{"message": "success", })}func InsertComment(c *gin.Context) { db := variable.DB uid := c.PostForm("uid") pid := c.PostForm("pid") text := c.PostForm("text") res, err := db.Exec(`INSERT INTO comments(uid,text,pid,date) values(?,?,?,?)`,uid, text, pid, time.Now().UnixMilli()) checkError(err) n, err := res.RowsAffected() checkError(err) if n == 0 {c.JSON(501, gin.H{"message": "failure",})c.Abort()return } n, err = res.LastInsertId() checkError(err) rows, err := db.Query(`select id,uid,text,pid,date,name from comments join users using(uid) where id = ?`, n) checkError(err) defer rows.Close() rows.Next() var cm structs.Comment rows.Scan(&cm.ID, &cm.UID, &cm.Text, &cm.Pid, &cm.Date, &cm.Name) c.JSON(200, cm)}
controllers/post.go 获取帖子信息文章插图
package controllerimport ( "github.com/gin-gonic/gin" "wolflong.com/vue_gin/structs" "wolflong.com/vue_gin/variable")func QueryPost(c *gin.Context) { db := variable.DB pid := c.Query("pid") rows, err := db.Query(`select id,bgcolor,textcolor,headimg,videosrc,imgs,html from posts where id = ?`, pid) checkError(err) defer rows.Close() var Post []structs.Post rows.Next() var g structs.Post err = rows.Scan(&g.ID, &g.Bgcolor, &g.Textcolor, &g.Headimg, &g.Videosrc, &g.Imgs, &g.Html) checkError(err) Post = append(Post, g) c.JSON(200, Post)}
router/router.go 配置路由组package routerimport ( "github.com/gin-gonic/gin" "wolflong.com/vue_gin/controller")func Router(r *gin.Engine) { r.GET("/", controller.HelloWorld) r.GET("/queryGameblog", controller.QueryGameBlog) r.GET("/queryGamelist", controller.QueryGameList) comment := r.Group("/comment") {comment.GET("/query", controller.QueryComment)comment.POST("/delete", controller.DeleteComment)comment.POST("/insert", controller.InsertComment) } post := r.Group("/post") {post.GET("/query", controller.QueryPost) }}
main.go 启动Ginpackage mainimport ( "github.com/gin-gonic/gin" "wolflong.com/vue_gin/router" "wolflong.com/vue_gin/utils")func init() { utils.MySqlDB()}func main() { r := gin.Default() router.Router(r) r.Run(":1314")}
GameView 详情页文章插图
代码仓库vue10" rel="external nofollow noreferrer">alicepolice/vue10 (github.com)
router/index.ts 动态路由修改路由代码,使其变为动态路由 。
{path: '/gameView/:pid',name: 'gameView',component: GameViewVue},
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 云原生之旅 - 9)云原生时代网关的后起之秀Envoy Proxy 和基于Envoy 的 Emissary Ingress
- 云原生之旅 - 8)云原生时代的网关 Ingress Nginx
- gta怎么加入好友战局(gta为什么别人进不来我的战局)
- Vue前端框架基础+Element的使用
- 云原生之旅 - 7)部署Terrform基础设施代码的自动化利器 Atlantis
- 7.Vue常用属性
- 丫头生日祝福语简短独特 祝我的小棉袄48岁生日快乐
- 9岁女儿生日文案高级简短 祝我的小棉袄生日快乐
- 女儿生日妈妈发朋友圈的句子 祝我的小棉袄生日快乐的文案
- 女儿生日文案高级简短 祝我的小棉袄生日快乐