文章目录
Grom之数据查询
注:本文是基于Windos系统上 gorm.io/gorm@v1.23.4、gorm.io/driver/mysql@v1.3.3进行讲解
1.First单条查询
1.1第一种
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
user := new(Student) //实例化对象
// SELECT * FROM users ORDER BY id LIMIT 1;
res := conn.First(user) //将查询数据传入到对象中
fmt.Println(res.Error) //判断返回值的错误
fmt.Println(res.RowsAffected) //查看返回条数
fmt.Println("查询到的对象为", *user)
fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出
}
运行代码前
1.2第二种
//以下代码也能输出以下内容
user := Student{}
res := conn.First(&user) //将查询数据传入到对象中
fmt.Println(res.Error) //判断返回值的错误
fmt.Println(res.RowsAffected) //查看返回条数
//fmt.Println("查询到的对象为", *user) 不能写成*user ,会编译失败,不能这样写
fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出
运行代码前
1.3第三种
var user = Student{}
res := conn.First(&user) //将查询数据传入到对象中
fmt.Println(res.Error) //判断返回值的错误
fmt.Println(res.RowsAffected) //查看返回条数
//fmt.Println("查询到的对象为", *user) 也不能写成*user ,会编译失败,不能这样写
fmt.Println("查询到的对象为", user) //相对于上面的语句建议使用这个输出
2.Find全部查询
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
users := []Student{} //实例化对象
//上面一行等效于 var users []Student =[]Studnet{}
conn.Find(&users) //将所有查询数据传入到对象中
//fmt.Println(*users) 也不能写成*users ,会编译失败,不能这样写
fmt.Println(users) //打印所有数据
}
运行代码前
3.Where条件查询
3.1=查询
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
user := new(Student)
conn.Where("age=?", 22).First(user)
fmt.Println(user)
fmt.Println(*user)
}
运行代码前
3.2结构体字段查询
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
stu := new(Student)
conn.Where(&Student{Name: "yang"}).Find(stu) //通过结构体的Name查找
fmt.Println(stu)
fmt.Println(*stu)
}
运行代码前
3.3or查询
3.3.1第一种
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
stus := []Student{}
res := conn.Where("name=? or name=?", "yang", "zhou").Find(&stus)
fmt.Println(res.Error)
fmt.Println(stus)
}
运行代码前
3.3.2第二种
res := conn.Where("name=?", "yang").Or("name=?", "zhou").Find(&stus)
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
3.4map查询
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
//使用dsn连接到数据库,grom自带的数据库池
//账号:密码@连接方式(ip地址:端口号)/数据库?语言方式,时区(未设置时区的话采用8小时制度)
dsn := "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local"
conn, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) //使用mysq连接数据库,第二个参数可以增加更多的配置(可有可无)
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
stu := new(Student)
res := conn.Where(map[string]interface{}{"name": "yang", "age": 22}).Find(&stu)
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stu)
}
运行代码前
4.Order排序查询
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
stus := []Student{}
res := conn.Order("id desc").Find(&stus) // desc是降序的
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
5.Order多条件排序查询
5.1第一种
conn.AutoMigrate(&Student{}) //创建表?判断是否表结构存在
stu := &Student{
Id: 5,
Name: "yang",
Age: 28,
}
re := conn.Create(stu) //向数据库中插入数据
if re.Error != nil { //判断是否插入数据出错
fmt.Println(re.Error)
}
stus := []Student{}
res := conn.Order("name").Order("age desc").Find(&stus) //名字是默认升序的;名字相同时年龄降序
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
运行代码后
5.2第二种
stus := []Student{}
res := conn.Order("name").Order("age").Find(&stus) //名字是默认升序的;名字相同时年龄颜色也是默认升序
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
6.Limit 查询
stus := []Student{}
res := conn.Limit(3).Find(&stus) //只查询3条数据,查询前三条数据
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
7.Offset查询
stus := []Student{}
res := conn.Limit(3).Offset(1).Find(&stus) //跳过第一条后,查询前3条数据
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
运行代码前
8.Distinct查询
stus := []Student{}
res := conn.Distinct("name").Find(&stus)
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stus)
//查看name ,并且不看重复值
9.FirstOrCreate创建并查询
stu := Student{}
res := conn.FirstOrCreate(&stu, Student{Name: "FirstorCreate"})
fmt.Println(res.Error)
fmt.Println(res.RowsAffected)
fmt.Println(stu)
运行代码前
运行代码后
10.Not查询
10.1第一种
stus := []Student{}
conn.Not("name = ?", "yang").First(&stus)
// 等效于: SELECT * FROM users WHERE name <> "yang" LIMIT 1;
fmt.Println(stus)
conn.Not("name = ?", "xin").First(&stus)
// 等效于: SELECT * FROM users WHERE name <> "xin" LIMIT 1;
fmt.Println(stus)
运行代码前
10.1第二种
stus := []Student{}
conn.Not(map[string]interface{}{"name": []string{"yang", "zhou"}}).Find(&stus)
// SELECT * FROM users WHERE name NOT IN ("yang", "zhou");
fmt.Println(stus)
conn.Not(map[string]interface{}{"name": []string{"jun", "xin"}}).Find(&stus)
// SELECT * FROM users WHERE name NOT IN ("jun", "xin");
fmt.Println(stus)
运行代码前
10.1第三种
stus := []Student{}
conn.Not(Student{Name: "yang", Age: 22}).First(&stus)
// SELECT * FROM users WHERE name <> "yang" AND age <> 22 ORDER BY id LIMIT 1;
fmt.Println(stus)
conn.Not(Student{Name: "zhou", Age: 22}).First(&stus)
// SELECT * FROM users WHERE name <> "zhou" AND age <> 22 ORDER BY id LIMIT 1;
fmt.Println(stus)
运行代码前
10.1第四种
stus := []Student{}
conn.Not([]int64{1, 2, 3}).First(&stus)
// SELECT * FROM users WHERE id NOT IN (1,2,3) ORDER BY id LIMIT 1;
fmt.Println(stus)
fmt.Println()
conn.Not([]int64{1, 2}).First(&stus)
// SELECT * FROM users WHERE id NOT IN (1,2) ORDER BY id LIMIT 1;
fmt.Println(stus)
运行代码前