1. 程式人生 > 其它 >Gin GORM 查詢語句和原生SQL的使用

Gin GORM 查詢語句和原生SQL的使用

Gin GORM 查詢語句和原生SQL的使用

package admin

import (
	"gindemo15/models"
	"github.com/gin-gonic/gin"
)

//測試的結構體
type NavJson struct {
	Id    int    `json:"id"`
	Title string `json:"title"`
}
//測試的結構體:指定表名稱
func (NavJson) TableName() string {
	return "nav"
}

type NavController struct {
	BaseController
}

func (con NavController) Index(c *gin.Context) {

	查詢全部資料
	navList := []models.Nav{}
	models.DB.Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	查詢一條資料
	
	navResult := models.Nav{Id: 21}
	models.DB.Find(&navResult)
	c.JSON(200, gin.H{
		"result": navResult,
	})
	
	/*
		Where條件
		=
		<
		>
		<=
		>=
		!=
		IS NOT NULL
		IS NULL
		BETWEEN AND
		NOT BETWEEN AND
		IN
		OR
		AND
		NOT
		LIKE
	*/
	
	查詢Id大於3的資料
	navList := []models.Nav{}
	models.DB.Where("id>3").Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	查詢 id大於3 並且 id小於9的資料
	
	var a = 3
	var b = 9
	navList := []models.Nav{}
	models.DB.Where("id>? AND id<?", a, b).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	使用in查詢 id 在 3, 5, 6中的資料
	
	navList := []models.Nav{}
	models.DB.Where("id in (?)", []int{3, 5, 6}).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	使用like查詢標題裡面包含 會 的內容
	var str ="%織%"
	navList := []models.Nav{}
	models.DB.Where("title like ?", str).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	查詢 id在3和9之間的資料 使用 between and
	
	navList := []models.Nav{}
	models.DB.Where("id between ? and ?", 3, 9).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	Or 查詢id=2 或者 id=3的資料
	navList := []models.Nav{}
	models.DB.Where("id =? OR id =?", 2, 3).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	Or 查詢id=2 或者 id=3 或者 id=4的資料
	navList := []models.Nav{}
	models.DB.Where("id =?", 2).Or("id = ?", 3).Or("id = ?", 4).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	使用Select指定返回的欄位
	navList := []NavJson{}//定義要返回的結構體
	models.DB.Select("id,title").Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	Order排序 、Limit 、Offset
	
	id倒序排序desc,升序使用asc
	navList := []models.Nav{}
	models.DB.Order("id desc").Order("sort asc").Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	navList := []models.Nav{}
	//Limit(2):只獲取前兩條
	models.DB.Order("id desc").Order("sort asc").Limit(2).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	分頁:跳過2條
	navList := []models.Nav{}
	models.DB.Order("id desc").Offset(2).Limit(2).Find(&navList)
	c.JSON(200, gin.H{
		"result": navList,
	})
	
	Count 統計總數
	navList := []models.Nav{}
	var num int64
	models.DB.Find(&navList).Count(&num)
	c.JSON(200, gin.H{
		"result": num,
	})
	
	使用原生 sql 刪除 user 表中的一條資料
	
	models.DB.Exec("delete from user where id=?", 5)
	
	使用原生 sql 修改 user 表中的一條資料
	
	models.DB.Exec("update user set username='itying gin grom' where id=?", 6)
	
	使用原生 sql查詢 uid=2 的資料
	
	userList := []models.User{}
	models.DB.Raw("select * from user where id >2").Scan(&userList)
	
	c.JSON(200, gin.H{
		"result": userList,
	})
	
	使用原生 查詢 User 表中所有的資料
	
	userList := []models.User{}
	models.DB.Raw("select * from user").Scan(&userList)
	
	c.JSON(200, gin.H{
		"result": userList,
	})
	
	統計 user 表的數量
	
	var num int
	models.DB.Raw("select count(1) from user").Scan(&num)
	c.JSON(200, gin.H{
		"result": num,
	})
	
	c.String(200, "Nav Index")
}