Gorm之数据插入

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)
}

image-20220210193013804

5.database\sql\包里sql.NullInt64结构体

image-20220210194129662

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)
}

image-20220210194202222

//注:虽然表里的是null值,但是查出来的是零值:
var n []User
conn.Raw("SELECT name, age FROM users WHERE name = ?", "lww").Scan(&n)
fmt.Println(n)

image-20220210203957554

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoGo在努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值