Gorm
GORM CRUD 資料庫的增刪改查
go get -u github.com/jinzhu/gorm
go get -u github.com/jinzhu/gorm/dialects/mysql
go常規查詢操作
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var db *gorm.DB
// Test 表結構
type Test struct {
ID int `json:"id" db:"id" gorm:"id"`
Username string `json:"username" db:"username" gorm:"username"`
Password string `json:"password" db:"password" gorm:"password"`
Datetime time.Time `json:"datetime" db:"datetime" gorm:"datetime"`
City string `json:"city" db:"city" gorm:"city"`
Country string `json:"country" db:"country" gorm:"country"`
Sex string `json:"sex" db:"sex" gorm:"sex"`
Age string `json:"age" db:"age" gorm:"age"`
}
// TableName 資料庫名稱規範
func (t Test) TableName() string {
return "test"
}
// Init xx
func Init() (err error) {
db, err = gorm.Open("mysql", "root:1qa2ws3ed@/gouse?charset=utf8&parseTime=True&loc=Local")
if err != nil {
fmt.Println("連線誒資料庫失敗")
return
}
return err
}
// SelectData 查詢單條語句
func SelectData() (err error) {
var info Test
// First根據主鍵查詢單條語句+++++++++++++
//_ = db.First(&info)
////fmt.Println(info.Username, info.Password, info.Age, info.Datetime)
//marshal, err := json.Marshal(info)
//if err != nil {
// fmt.Println("反序列化失敗")
// return
//
//}
//fmt.Println(string(marshal))
// 隨機獲取條資料++++++++++++++++
//db.Take(&info)
//TakeData, err := json.Marshal(info)
//fmt.Println(string(TakeData))
//// 根據主鍵查詢最後一條記錄+++++++++++++
//db.Last(&info)
//LastData, err := json.Marshal(info)
//fmt.Println(string(LastData))
//// 獲取所有記錄+++++++++++++++
//var infoList []Test
//
//db.Find(&info)
//FindData, err := json.Marshal(infoList)
//fmt.Println(string(FindData))
// First 查詢主鍵為10000的資料(僅當主鍵為int時)
db.First(&info, 10000)
marshal, err := json.Marshal(info)
fmt.Println(string(marshal))
return err
}
// WhereData where語句查詢
func WhereData() (err error) {
// where 條件查詢sex 為女的主鍵第一個
//var info Test
//db.Where("sex = ?", "女").First(&info)
//marshal, err := json.Marshal(info)
//fmt.Println(string(marshal))
// 查詢所有sex為男的使用者 並統計計數
var infoList []Test
//db.Where("sex = ?", "男").Find(&infoList)
//fmt.Println(len(infoList))
db.Where("id in (?)", []int{1000, 2000}).Find(&infoList)
marshal, err := json.Marshal(infoList)
fmt.Println(string(marshal))
return err
}
// main 主函式
func main() {
_ = Init()
//err := SelectData()
err := WhereData()
if err != nil {
fmt.Println("")
return
}
}
// like 查詢
db.Where("username like ?", "%abc%").Find(&infoList)
LikeData, err := json.Marshal(infoList)
fmt.Println(string(LikeData))
return err
注: ?其實就是佔位符
- go結構體轉換成map
go map 查詢
db.Where(map[string]interface{}{"id": 1000}).Find(&info)
MapData, err := json.Marshal(info)
fmt.Println(string(MapData))
return err
go not查詢
// NotSelect Not查詢用法
func NotSelect() (err error) {
var infoList []Test
db.Not("id = ?", "1").First(&infoList)
NotLikeData, err := json.Marshal(infoList)
fmt.Println(string(NotLikeData))
db.Not("id in (?)", []int{1, 2, 3, 4, 5}).First(&infoList)
notinData, err := json.Marshal(infoList)
fmt.Println(string(notinData))
return err
}
go or 查詢
// OrSelect or查詢
func OrSelect() (err error) {
var infoList []Test
db.Where("id = ?", "1").Or("id = ?", "2").Find(&infoList)
OrData, err := json.Marshal(infoList)
fmt.Println(string(OrData))
return err
}
go 內聯查詢 等同於where條件
// 內聯條件
func NeiLian() (err error) {
var info Test
db.Find(&info, "id = ?", "1")
marshal, err := json.Marshal(info)
fmt.Println(string(marshal))
return err
}
Go firstorcreate
獲取匹配的第一條記錄,否則根據給定的條件建立一個新的記錄(僅支援struct or map)
// FirstOrCreate 查詢條件後的第一條語句 如果為空 就建立一條記錄
func FirstOrCreate() (err error) {
var info Test
db.Where("id = ?", "1000").FirstOrCreate(&info)
marshal, err := json.Marshal(info)
fmt.Println(string(marshal))
return err
}