title: gin 增删改查
创建数据
router.POST("/user", func(c *gin.Context) {
var buffer bytes.Buffer
name := c.PostForm("name")
age := c.PostForm("age")
stmt, err := db.Prepare("insert into user (name, age) values(?,?);")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(name, age)
if err != nil {
fmt.Print(err.Error())
}
buffer.WriteString(name)
buffer.WriteString(" ")
buffer.WriteString(age)
defer stmt.Close()
username := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf(" %s successfully created", username),
})
})
更新用户
router.PUT("/user", func(c *gin.Context) {
var buffer bytes.Buffer
id := c.Query("id")
name := c.PostForm("name")
age := c.PostForm("age")
stmt, err := db.Prepare("update person set name= ?, age= ? where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(name, age, id)
if err != nil {
fmt.Print(err.Error())
}
buffer.WriteString(name)
buffer.WriteString(" ")
buffer.WriteString(age)
defer stmt.Close()
username := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully updated to %s", username),
})
})
删除用户
router.DELETE("/user", func(c *gin.Context) {
id := c.Query("id")
stmt, err := db.Prepare("delete from user where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(id)
if err != nil {
fmt.Print(err.Error())
}
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully deleted user: %s", id),
})
})
查询所有用户
router.GET("/user", func(c *gin.Context) {
var (
user User
users []User
)
rows, err := db.Query("select id, name, last_name from user;")
if err != nil {
fmt.Print(err.Error())
}
for rows.Next() {
err = rows.Scan(&user.id, &user.name, &user.name)
users = append(users, user)
if err != nil {
fmt.Print(err.Error())
}
}
defer rows.Close()
c.JSON(http.StatusOK, gin.H{
"result": users,
"count": len(users),
})
})
查询一条数据
router.GET("/user/:id", func(c *gin.Context) {
var (
user User
result gin.H
)
id := c.Param("id")
row := db.QueryRow("select id, name, age from user where id = ?;", id)
err = row.Scan(&user.id, &user.name, &user.age)
if err != nil {
result = gin.H{
"result": nil,
"count": 0,
}
} else {
result = gin.H{
"result": user,
"count": 1,
}
}
c.JSON(http.StatusOK, result)
})
所有代码
package main
import (
"bytes"
"database/sql"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/starkgo")
if err != nil {
fmt.Print(err.Error())
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Print(err.Error())
}
type User struct {
id int
name string
age string
}
router := gin.Default()
router.GET("/user/:id", func(c *gin.Context) {
var (
user User
result gin.H
)
id := c.Param("id")
row := db.QueryRow("select id, name, age from user where id = ?;", id)
err = row.Scan(&user.id, &user.name, &user.age)
if err != nil {
result = gin.H{
"result": nil,
"count": 0,
}
} else {
result = gin.H{
"result": user,
"count": 1,
}
}
c.JSON(http.StatusOK, result)
})
router.GET("/user", func(c *gin.Context) {
var (
user User
users []User
)
rows, err := db.Query("select id, name, last_name from user;")
if err != nil {
fmt.Print(err.Error())
}
for rows.Next() {
err = rows.Scan(&user.id, &user.name, &user.name)
users = append(users, user)
if err != nil {
fmt.Print(err.Error())
}
}
defer rows.Close()
c.JSON(http.StatusOK, gin.H{
"result": users,
"count": len(users),
})
})
router.POST("/user", func(c *gin.Context) {
var buffer bytes.Buffer
name := c.PostForm("name")
age := c.PostForm("age")
stmt, err := db.Prepare("insert into user (name, age) values(?,?);")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(name, age)
if err != nil {
fmt.Print(err.Error())
}
buffer.WriteString(name)
buffer.WriteString(" ")
buffer.WriteString(age)
defer stmt.Close()
username := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf(" %s successfully created", username),
})
})
router.PUT("/user", func(c *gin.Context) {
var buffer bytes.Buffer
id := c.Query("id")
name := c.PostForm("name")
age := c.PostForm("age")
stmt, err := db.Prepare("update person set name= ?, age= ? where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(name, age, id)
if err != nil {
fmt.Print(err.Error())
}
buffer.WriteString(name)
buffer.WriteString(" ")
buffer.WriteString(age)
defer stmt.Close()
username := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully updated to %s", username),
})
})
router.DELETE("/user", func(c *gin.Context) {
id := c.Query("id")
stmt, err := db.Prepare("delete from user where id= ?;")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(id)
if err != nil {
fmt.Print(err.Error())
}
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Successfully deleted user: %s", id),
})
})
router.Run(":3000")
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。