目錄
- Gin+Gorm小專案
- 創建專案
- 參考靜態檔案
- 搭建架子
- 創建資料庫
- 添加功能
- 查找功能
- 修改功能
- 洗掉功能
- 總代碼
Gin+Gorm小專案
創建專案


E:\gostudent\gin\bubble>go mod tidy //增加缺失的包,移除沒用的包
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
r := gin.Default()
//告訴gin框架去哪里找模板檔案
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.Run(":9090")
}

參考靜態檔案



搭建架子
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
//Tode Model
type Tode struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
func main() {
//創建資料庫
//sql: CREATE DATABASE bubble;
//連接資料庫
//遇事不決寫注釋
r := gin.Default()
//告訴gin框架模板檔案參考的靜態檔案去哪里找
r.Static("/static","static")
//告訴gin框架去哪里找模板檔案
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
//v1 api
v1Group := r.Group("v1")
{
//待辦事項
//添加
v1Group.POST("/todo", func(c *gin.Context) {
})
//查看所有的代辦事項
v1Group.GET("/todo", func(c *gin.Context) {
})
//查看某一個代辦事項
v1Group.GET("/todo/:id", func(c *gin.Context) {
})
//修改某一個待辦事項
v1Group.PUT("/todo/:id", func(c *gin.Context) {
})
//洗掉
v1Group.DELETE("/todo/:id", func(c *gin.Context) {
})
}
r.Run(":9090")
}
創建資料庫

func initMySQL() (err error) {
dsn := "root:123456@tcp(127.0.0.1:3306)/bubble?charset=utf8mb4&parseTime=True&loc=Local"
DB, err = gorm.Open("mysql",dsn)
//測驗連通性
err = DB.DB().Ping()
return
//return DB.DB().Ping()
}
func main() {
//創建資料庫
//sql: CREATE DATABASE bubble;
//連接資料庫
err := initMySQL()
if err != nil {
//可以搞個日志
panic(err)
}
//模型系結
DB.AutoMigrate(&Tode{}) //todos
defer DB.Close() //程式退出,關閉資料庫
添加功能
//待辦事項
//添加
v1Group.POST("/todo", func(c *gin.Context) {
//前端頁面填寫待辦事項,點擊提交,會發請求到這里
//1. 從請求中把資料拿出來
var todo Tode
c.BindJSON(&todo)
//2. 存入資料庫
err = DB.Create(&todo).Error
//3. 回傳回應
if err != nil {
c.JSON(http.StatusOK,gin.H{"error": err.Error()})
}else {
c.JSON(http.StatusOK, todo)
//c.JSON(http.StatusOK, gin.H{
// "code": 200,
// "msg" : "success",
// "data": todo,
//})
}


查找功能
//查看所有的代辦事項
v1Group.GET("/todo", func(c *gin.Context) {
//查詢todo這個表里的所有資料
var todeList []Tode
err := DB.Find(&todeList).Error
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else {
c.JSON(http.StatusOK, todeList)
}
})
//查看某一個代辦事項
v1Group.GET("/todo/:id", func(c *gin.Context) {
})

修改功能
//修改某一個待辦事項
v1Group.PUT("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "無效的id"})
return
}
var todo Todo
if err = DB.Where("id=?", id).First(&todo).Error; err!=nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
c.BindJSON(&todo)
if err = DB.Save(&todo).Error; err!= nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, todo)
}
})
洗掉功能
//洗掉
v1Group.DELETE("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "無效的id"})
return
}
if err = DB.Where("id=?", id).Delete(Todo{}).Error;err!=nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, gin.H{id:"deleted"})
}
})
總代碼
package main
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"net/http"
)
var (
DB *gorm.DB
)
//Tode Model
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
func initMySQL() (err error) {
dsn := "root:123456@tcp(127.0.0.1:3306)/bubble?charset=utf8mb4&parseTime=True&loc=Local"
DB, err = gorm.Open("mysql",dsn)
//測驗連通性
err = DB.DB().Ping()
return
//return DB.DB().Ping()
}
func main() {
//創建資料庫
//sql: CREATE DATABASE bubble;
//連接資料庫
err := initMySQL()
if err != nil {
//可以搞個日志
panic(err)
}
//模型系結
DB.AutoMigrate(&Todo{}) //todos
defer DB.Close() //程式退出,關閉資料庫
//遇事不決寫注釋
r := gin.Default()
//告訴gin框架模板檔案參考的靜態檔案去哪里找
r.Static("/static","static")
//告訴gin框架去哪里找模板檔案
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
//v1 api
v1Group := r.Group("v1")
{
//待辦事項
//添加
v1Group.POST("/todo", func(c *gin.Context) {
//前端頁面填寫待辦事項,點擊提交,會發請求到這里
//1. 從請求中把資料拿出來
var todo Todo
c.BindJSON(&todo)
//2. 存入資料庫
err := DB.Create(&todo).Error
if err != nil {
c.JSON(http.StatusOK,gin.H{"error": err.Error()})
}else {
c.JSON(http.StatusOK, todo)
//c.JSON(http.StatusOK, gin.H{
// "code": 200,
// "msg" : "success",
// "data": todo,
//})
}
//3. 回傳回應
})
//查看所有的代辦事項
v1Group.GET("/todo", func(c *gin.Context) {
//查詢todo這個表里的所有資料
var todeList []Todo
err := DB.Find(&todeList).Error
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else {
c.JSON(http.StatusOK, todeList)
}
})
//查看某一個代辦事項
v1Group.GET("/todo/:id", func(c *gin.Context) {
})
//修改某一個待辦事項
v1Group.PUT("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "無效的id"})
return
}
var todo Todo
if err = DB.Where("id=?", id).First(&todo).Error; err!=nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
c.BindJSON(&todo)
if err = DB.Save(&todo).Error; err!= nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, todo)
}
})
//洗掉
v1Group.DELETE("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "無效的id"})
return
}
if err = DB.Where("id=?", id).Delete(Todo{}).Error;err!=nil{
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
}else{
c.JSON(http.StatusOK, gin.H{id:"deleted"})
}
})
}
r.Run(":9090")
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/17383.html
標籤:Go
下一篇:企業級專案結構拆分
