Gorm之数据插入
注:本文是基于Windos系统上 gorm.io/gorm@v1.23.4、gorm.io/driver/mysql@v1.3.3进行讲解
1.全字段属性插入
Creat结构体全字段属性插入
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 := &Student{
Id: 1,
Name: "yang",
Age: 22,
}
res := conn.Create(stu) //向数据库中插入数据 ,它的返回值只有一个类型,没有error类型
//注:如果上面写成stu := Student{...},则这里写成Create(&stu)
if res.Error != nil { //判断是否插入数据出错
fmt.Println(res.Error)
}
}
//控制台点击运行后控制台不会有任何输出
代码运行前:
代码运行后:
2.部分字段属性插入
Select结构体部分字段属性插入
package main
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
//模型结构
type Student struct {
Id int
Name string
Age int
}
func main() {
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{})
if err != nil {
fmt.Println(err)
}
conn.AutoMigrate(&Student{})
stu := &Student{
Id: 2,
Name: "xin",
}
res := conn.Select("Id", "Name").Create(stu)
if res.Error != nil { //判断是否插入数据出错
fmt.Println(res.Error)
}
}
//控制台点击运行后控制台不会有任何输出
代码运行前
代码运行后:
3.批量插入
Creat批量插入
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 := []Student{
{Name: "jun", Age: 24},
{Name: "zhou", Age: 26},
}
conn.Create(&stu)//传入stu的地址
//控制台点击运行后控制台不会有任何输出
}
代码运行前:
代码运行后:
4.插入注意事项
虽然grom默认创建的表的字段默认是NULL,但create创建的时候时给到零值的
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string
Age int
}
func main() {
conn, _ := gorm.Open(mysql.New(mysql.Config{
DSN: "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local",
DefaultStringSize: 171,
}), &gorm.Config{})
conn.AutoMigrate(&User{})
users := []User{{Name: "adsa"},
{Age: 11},
}
conn.Create(&users)
}
5.database\sql\包里sql.NullInt64结构体
package main
import (
"database/sql"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
type User struct {
gorm.Model
Name string
Age sql.NullInt64
}
func main() {
conn, _ := gorm.Open(mysql.New(mysql.Config{
DSN: "root:414524@tcp(127.0.0.1:3306)/gotest?charset=utf8mb4&parseTime=True&loc=Local",
DefaultStringSize: 171,
}), &gorm.Config{})
conn.AutoMigrate(&User{})
users := []User{{Name: "adsa", Age: sql.NullInt64{Int64: 788, Valid: true}},
{Name: "lww", Age: sql.NullInt64{Int64: 5555555, Valid: true}},
{Name: "lww"},
{Name: "lww"},
}
conn.Create(&users)
}
//注:虽然表里的是null值,但是查出来的是零值:
var n []User
conn.Raw("SELECT name, age FROM users WHERE name = ?", "lww").Scan(&n)
fmt.Println(n)