运行规律
url --> controller --> logic或者(service)--> model
请求来了 --> 控制器 -->业务逻辑 -->模型层的增删改查
package controller
import (
"demo27/models"
"github.com/gin-gonic/gin"
"net/http"
)
/*
url --> controller --> logic或者(service) --> model
请求来了 --> 控制器 -->业务逻辑 -->模型层的增删改查
*/
func IndexHandler(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
func AddTodo(c *gin.Context) {
//前端页面填写代办事项 点击提交 会发送请求到这
//1 从请求中把数据拿出来
var todo models.Todo
c.BindJSON(&todo)
//2 存入数据库 //3 返回响应
err := models.CreateTodo(&todo)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{
"code": 200,
"msg": "success",
"data": todo,
})
}
}
func SelectTodoList(c *gin.Context) {
//查询所有的代办事项
todoList, err := models.GetTodoList()
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, todoList)
}
}
func UpdateTodo(c *gin.Context) {
//获取id
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无效的id"})
return
}
todo, err := models.GetTodo(id)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
c.BindJSON(&todo)
//保存到数据库
if err := models.UpdateTdo(todo); err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, todo)
}
}
func DeleteTodo(c *gin.Context) {
//获取id
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无效的id"})
return
}
err := models.DeleteTodo(id)
if err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{id: "deleted"})
}
}
mysql.go
package dao
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var (
DB *gorm.DB
)
func InitMySQL() (err error) {
dsn := "root:W8vFu%PmMuyESkjf@tcp(10.100.100.114:3306)/bubble?charset=utf8&parseTime=True"
//连接mysql数据库
DB, err = gorm.Open("mysql", dsn) //引用全局变量DB,这里不要加:
if err != nil {
panic(err)
}
return DB.DB().Ping()
}
Todo.go
package models
import "demo27/dao"
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
// Tode 这个model的增删改查操作都放在这里
func CreateTodo(todo *Todo) (err error) {
if err = dao.DB.Create(todo).Error; err != nil {
return err
}
return
}
func GetTodoList() (todoList []*Todo, err error) {
if err = dao.DB.Find(&todoList).Error; err != nil {
return nil, err
}
return
}
func GetTodo(id string) (todo *Todo, err error) {
todo = new(Todo)
if err = dao.DB.Where("id=?", id).Find(todo).Error; err != nil {
return nil, err
}
return
}
func UpdateTdo(todo *Todo) (err error) {
err = dao.DB.Save(todo).Error
return err
}
func DeleteTodo(id string) (err error) {
err = dao.DB.Where("id=?", id).Delete(&Todo{}).Error
return err
}
package routers
import (
"demo27/controller"
"github.com/gin-gonic/gin"
)
func SetupRouter() *gin.Engine {
r := gin.Default()
r.Static("/static", "static")
r.LoadHTMLGlob("templates/*")
r.GET("/", controller.IndexHandler)
v1Group := r.Group("v1")
{
//代办事项
//添加
v1Group.POST("/todo", controller.AddTodo)
//查看所有代办事项
v1Group.GET("/todo", controller.SelectTodoList)
//修改
v1Group.PUT("/todo/:id", controller.UpdateTodo)
//删除
v1Group.DELETE("/todo/:id", controller.DeleteTodo)
}
return r
}
链接:https://pan.baidu.com/s/1qdEQOf_RNpWyPnCe_XERfw?pwd=5nbn
提取码:5nbn
index.html
DOCTYPE html>
<html lang=en>
<head><meta charset=utf-8>
<meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1">
<link rel=icon href=/static/favicon.ico>
<title>bubble清单title>
<link href=/static/css/app.8eeeaf31.css rel=preload as=style>
<link href=/static/css/chunk-vendors.57db8905.css rel=preload as=style>
<link href=/static/js/app.007f9690.js rel=preload as=script>
<link href=/static/js/chunk-vendors.ddcb6f91.js rel=preload as=script>
<link href=/static/css/chunk-vendors.57db8905.css rel=stylesheet>
<link href=/static/css/app.8eeeaf31.css rel=stylesheet>
head>
<body>
<noscript>
<strong>We're sorry but dsb doesn't work properly without JavaScript enabled. Please enable it to continue.strong>
noscript>
<div id=app>div>
<script src=/static/js/chunk-vendors.ddcb6f91.js>script>
<script src=/static/js/app.007f9690.js>script>
body>
html>
package main
import (
"demo27/dao"
"demo27/models"
"demo27/routers"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
//连接数据库
err := dao.InitMySQL()
if err != nil {
panic(err)
}
//程序退出关闭数据库连接
defer dao.DB.Close()
//模型绑定,新建表
dao.DB.AutoMigrate(&models.Todo{})
//注册路由
r := routers.SetupRouter()
r.Run(":9090")
}
参考博客
https://www.liwenzhou.com/posts/Go/gin/