我的Vue之旅 09 数据数据库表的存储与获取实现 Mysql + Golang

第四期 · 将部分数据存储至Mysql , 使用axios通过golang搭建的http服务器获取数据 。
新建数据库DROP DATABASE VUE;create database if not exists vue;use vue;JSON TO MYSQLJSON to MySQL (transform.tools)
DROP DATABASE VUE;create database if not exists vue;use vue;CREATE TABLE gameblog (id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(255),text VARCHAR(255),img VARCHAR(255));insert into gameblog(title,text,img) values("Games of the Month: surrealist solitaire puzzles","What’s that? You need more games? I hear you, anonymous hapi fan.We’ve reached the part of the year when games start coming out fast","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102184434_1.jpg"),("Games of the Month: Puzzles!","Sometimes you need a good puzzle game, just something to throw all of your attention at and ignore anything else going on. Well if that sometime for you is right now, then you’re in luck because in this Games of the Month","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102184434_2.jpg"),("The next hapi Creator Day is July 29th!","I don’t think I’m allowed to make the entire body of this post “Thenext itch.io Creator Day is taking place on Friday July 29th.” I mean it’s true, we are hosting the next itch.io Creator Day on Friday July 29th but I should probably write more here.","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102184434_3.jpg");select * from gameblog;CREATE TABLE game (id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(255),text VARCHAR(255),img VARCHAR(255),price decimal(6,2) default 0,web boolean default 0# TODO 发布时间# TODO 浏览量# TODO 评论量# TODO 热度综合指标);CREATE TABLE tag (id INT PRIMARY KEY AUTO_INCREMENT,title VARCHAR(255));CREATE TABLE gametag (gameid INT,tagid INT);# TODO 外键insert into game(id,title,text,img,price,web) values(1,"Late Night Mop","A haunted house cleaning simulator.","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102193135_1.png",0,0),(2,"an average day at the cat cafe","A haunted house cleaning simulator.","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102193135_2.png",0,1),(3,"Corebreaker","A fast-paced action-platform shooter game with roguelike elements.","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102193135_3.png",19.99,0),(4,"Atuel","Traverse a surrealist landscape inspired by the Atuel River in Argentina.","https://xiaonenglife.oss-cn-hangzhou.aliyuncs.com/static/pic/2022/11/20221102193135_5.png",0,0);insert into tag values(1,"Difficult"),(2,"Fast-Paced");insert into gametag values(3,1),(3,2),(4,1);DELIMITER $$CREATE PROCEDURE gamelist()BEGIN # TODOEND $$DELIMITER ;select a.title,a.text,img,price,web,if(group_concat(c.title separator "#") is null ,"", group_concat(c.title separator "#")) as tag from game a left join gametag b on a.id = b.gameid left join tag c on b.tagid = c.id group by a.id;本地图片上传OSS图床得到静态资源的持久地址,我使用的是PicGo图床工具 。

我的Vue之旅 09 数据数据库表的存储与获取实现 Mysql + Golang

文章插图
SQL TO GOLANG STRUCT在线sql转golang struct - 球儿工具 (qetool.com)
config.go为了方便mysql服务器的配置,写一个配置文件 。
package mysql_vueimport "database/sql"func GetMySQLDB() (db *sql.DB, err error) { dbDriver := "mysql" dbUser := "root" dbPass := "sql2008" dbName := "vue" db, err = sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName) return}gameblog.goid暂时不需要 , 后期路由跳转需要用到,可以先注释 。
package mysql_vueimport ( "encoding/json" _ "github.com/go-sql-driver/mysql")type Gameblog struct { // ID int64 `db:"id" json:"id"` Title string `db:"title" json:"title"` Textstring `db:"text" json:"text"` Imgstring `db:"img" json:"img"`}func (Gameblog) TableName() string { return "gameblog"}func (Gameblog) QueryGameblog() (json_ []byte, err error) { // db, err := sql.Open("mysql", "root:sql2008@tcp(127.0.0.1:3306)/vue") db, err := GetMySQLDB() checkError(err) defer db.Close() // ^ 必须按照顺序选取,下面的Scan需要一一对应,如果多了或少了字段会导致Scan错误. results, err := db.Query("SELECT title,text,img FROM gameblog order by id desc") checkError(err) var gameBlogs []Gameblog for results.Next() {var gameBlog Gameblogerr = results.Scan(&gameBlog.Title, &gameBlog.Text, &gameBlog.Img)checkError(err)gameBlogs = append(gameBlogs, gameBlog) } json_, err = json.Marshal(gameBlogs) checkError(err) return json_, nil}httpSimplify server.go前面我们把评论相关的请求处理代码写在了 server.go,移出到 comment.go,并在init初始化中绑定各个请求路径处理函数 。
comment.gopackage serverimport ( "fmt" "net/http" "strconv")type Comment interface { QueryComment(pid int64) (json_ []byte, err error) InsertComment(uid, pid int64, text string) (json_ []byte, err error) DeleteComment(id int64) error}func init() { http.HandleFunc("/insertComment", insertComment) http.HandleFunc("/deleteComment", deleteComment) http.HandleFunc("/queryComment", queryComment)}func insertComment(w http.ResponseWriter, r *http.Request) { ....}func deleteComment(w http.ResponseWriter, r *http.Request) { ....}func queryComment(w http.ResponseWriter, r *http.Request) { ....}

推荐阅读