前期工作可以看之前的(连接数据库;以及确定要操作的库)
Gin之GORM 操作数据库(MySQL)-CSDN博客https://blog.csdn.net/m0_72264240/article/details/134948202?spm=1001.2014.3001.5502这次我们操作gin库下的另外一个表mysqll表
mysqlcontroller.go:
package admin
import (
"gindemo04/models"
"net/http"
"github.com/gin-gonic/gin"
)
type MysqlController struct {
// Create a new instance of the BaseController struct
BaseController
}
func (con MysqlController) Index(c *gin.Context) {
}
package routers
import (
"gindemo04/controllers/admin"
"gindemo04/middle"
"github.com/gin-gonic/gin"
)
func AdminRoutersInit(r *gin.Engine) {
//middlewares.InitMiddleware中间件
adminRouters := r.Group("/admin", middle.InitMiddleware)
{
adminRouters.GET("/mysqll",admin.MysqlController{}.Index)
}
}
package models
type Mysqll struct{ //默认操作的是mysqlls这个表
Id int
Master string
Servant string
RealFeature string
}
func (Mysqll) TableName() string {
return "mysqll"
}
前期操作完成
查询 | GORM - The fantastic ORM library for Golang, aims to be developer friendly.https://gorm.io/zh_CN/docs/query.html
//1、查询全部数据
// This function is used to return the index page of the application
//先搞一个数据库的切片
mysqlList := []models.Mysqll{}
//查询数据库数据
models.DB.Find(&mysqlList)
c.JSON(http.StatusOK, gin.H{
"result": mysqlList,
})
mysqlList1 := []models.Mysqll{}
models.DB.Where("id = ?", 1).Find(&mysqlList1)
c.JSON(http.StatusOK, gin.H{
"result": mysqlList1,
})
mysqlResult := models.Mysqll{Id: 2}
models.DB.Find(&mysqlResult)
c.JSON(http.StatusOK, gin.H{
"result": mysqlResult,
})
mysqlList3 := []models.Mysqll{}
models.DB.Where("id > ?", 3).Find (&mysqlList3)
c.JSON(http.StatusOK, gin.H{
"id>3的是": mysqlList3,
})
// var a =1
// var b =3
//这样修改需要大于小于的即可(未操作)
mysqlList4 := []models.Mysqll{}
models.DB.Where("id > ? and id < ?", 1, 3).Find(&mysqlList4)
c.JSON(http.StatusOK, gin.H{
"id>1 and id < 3": mysqlList4,
})
mysqlList5 := []models.Mysqll{}
models.DB.Where("id in (?)", []int{1, 3, 4}).Find(&mysqlList5)
c.JSON(http.StatusOK, gin.H{
"id in (1,3,4)": mysqlList5,
})
mysqlList6 := []models.Mysqll{}
models.DB.Where("servant like ?", "%r%").Find (&mysqlList6)
c.JSON(http.StatusOK, gin.H{
"servant like %r%": mysqlList6,
})
mysqlList7 := []models.Mysqll{}
models.DB.Where("id between ? and ?", 1, 4).Find(&mysqlList7)
c.JSON(http.StatusOK, gin.H{
"id between 1 and 4": mysqlList7,})
mysqlList8 := []models.Mysqll{}
models.DB.Where("id = ? or id = ?", 2, 3).Find(&mysqlList8)
c.JSON(http.StatusOK, gin.H{
"id = 2 or id = 3": mysqlList8,
})
mysqlList9 := []models.Mysqll{}
models.DB.Where("id =?", 2).Or("id = ?", 3).Or("id = ?", 4).Find(&mysqlList9)
c.JSON(http.StatusOK, gin.H{
"id = 2 or id = 3 or id = 4": mysqlList9,
})
mysqlLIst10 := []models.Mysqll{}
models.DB.Select("id,servant").Find(&mysqlLIst10)
c.JSON(http.StatusOK, gin.H{
"id,servant": mysqlLIst10,
})
注意:如果需要只显示这两个,需要从新定义一个结构体,只有这两个字段且需要重新搞tablename指定
//limit限制取的数据数量
//offset 偏移量,确定跳过的数据量(通常和limit配合使用,完成分页(跨越)操作)
mysqlList11 := []models.Mysqll{}
models.DB.Order("id desc").Limit(2).Offset(1).Find(&mysqlList11)
c.JSON(http.StatusOK, gin.H{
"id desc": mysqlList11,
})
mysqlList12 := []models.Mysqll{}
var count int64
models.DB.Find(&mysqlList12).Count(&count)
c.JSON(http.StatusOK, gin.H{
"count": count,
})
mysqlList13 := []models.Mysqll{}
var count1 int64
models.DB.Where("id > ?", 1).Find(&mysqlList13).Count(&count1)
c.JSON(http.StatusOK, gin.H{
"id大于1": count1,
})
SQL 构建器 | GORM - The fantastic ORM library for Golang, aims to be developer friendly.https://gorm.io/zh_CN/docs/sql_builder.html
result := models.DB.Exec("delete from user where id = ?", 1)
fmt.Println(result.RowsAffected)
result2 := models.DB.Exec("update user set username = ? where id = ?", "卫宫切嗣", 2)
fmt.Println(result2.RowsAffected)
mysqlList14 := []models.Mysqll{}
models.DB.Raw("select * from mysqll where id = ?", 2).Scan(&mysqlList14)
c.JSON(http.StatusOK, gin.H{
"id=2": mysqlList14,
})
mysqlList15 := []models.Mysqll{}
models.DB.Raw("select * from mysqll").Scan(&mysqlList15)
c.JSON(http.StatusOK, gin.H{
"mysqll": mysqlList15,
})
var count3 int64
models.DB.Raw("select count(*) from mysqll").Scan(&count3)
c.JSON(http.StatusOK, gin.H{
"count": count3,
}