golang gin使用xorm+mysql 实现简单增删改查

效果图

golang gin使用xorm+mysql 实现简单增删改查_第1张图片

核心代码

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"net/http"
	"time"
)

var x *xorm.Engine
var xormResponse XormResponse

//定义结构体,没有表  会进行创建
type Person struct {
	Id      int       `xorm:"pk autoincr" json:"id"`
	Name    string    `xorm:"unique" json:"name"`
	Age     uint8     `json:"age"`
	Created time.Time `xorm:"created" json:"created"`
	Updated time.Time `xorm:"updated" json:"updated"`
}

// 响应
type XormResponse struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func init() {

	strStr := "golang:golang@tcp(127.0.0.1:3306)/golang?charset=utf8" // /golang database name

	var err error
	x, err = xorm.NewEngine("mysql", strStr)

	if err != nil {
		fmt.Println("数据库连接失败:", err)
	}

	err = x.Sync(new(Person))

	if err != nil {
		fmt.Println("数据表同步失败:", err)
	}

}

func main() {

	r := gin.Default()

	r.POST("/insert", xormInsertData)
	r.GET("/get", xormGetData) //查询一记录
	r.GET("/mulget", xormGetMulData) // 查询多条记录

	r.PUT("update",xormUpdateData)
	r.DELETE("delete",xormdeleteData)
	r.Run()

}

func xormdeleteData(c *gin.Context) {

	name := c.Query("name")
	i, err := x.Where("name=?", name).Delete(&Person{})
	if err != nil {

	}
	fmt.Println(i)

}

func xormUpdateData(c *gin.Context) {

	var p Person
	err := c.Bind(&p)
	if err != nil {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "参数错误,"+err.Error()
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}


	var person []Person
	err = x.Where("name=?", p.Name).Find(&person)
	if err != nil || len(person) <= 0 {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "数据不存在,"+err.Error()
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}

	update, err := x.Where("name=?", p.Name).Update(&Person{Name: p.Name, Age: p.Age})
	if err != nil || update <= 0 {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "更新失败,"+err.Error()
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}

	xormResponse.Code = http.StatusOK
	xormResponse.Msg = "更新成功"
	xormResponse.Data = "success"

	fmt.Println(xormResponse)
	c.JSON(http.StatusOK, xormResponse)


}

func xormGetMulData(c *gin.Context) {

	//name := c.Query("name")
	var person []Person

	err := x.Where("age>20").Limit(10, 0).Asc("age").Find(&person)

	if err != nil {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "查询失败,"+err.Error()
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}

	xormResponse.Code = http.StatusOK
	xormResponse.Msg = "查询成功"
	xormResponse.Data = person
	c.JSON(http.StatusOK, xormResponse)
	return



}

func xormGetData(c *gin.Context) {

	name := c.Query("name")

	var person []Person
	err := x.Where("name=?", name).Find(&person)
	if err != nil {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "查询失败,"+err.Error()
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}

	xormResponse.Code = http.StatusOK
	xormResponse.Msg = "查询成功"
	xormResponse.Data = person
	c.JSON(http.StatusOK, xormResponse)
	return
}


func xormInsertData(c *gin.Context) {

	var person Person

	err := c.Bind(&person)
	if err != nil {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "参数错误"
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}

	affected, err := x.Insert(person)

	if err != nil || affected <= 0 {
		xormResponse.Code = http.StatusBadGateway
		xormResponse.Msg = "添加数据失败"
		xormResponse.Data = "error"
		c.JSON(http.StatusOK, xormResponse)
		return
	}


	xormResponse.Code = http.StatusOK
	xormResponse.Msg = "添加数据成功"
	xormResponse.Data = "success"
	c.JSON(http.StatusOK, xormResponse)
	fmt.Println(affected)
	return

}



你可能感兴趣的:(go,gin,xorm,go)