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
}