创建记录 package mainimport ("database/sql""fmt""gORM.io/driver/Mysql""Gorm.io/gorm""gorm.io/gorm/logger""log""os""time")typ
package mainimport ("database/sql""fmt""gORM.io/driver/Mysql""Gorm.io/gorm""gorm.io/gorm/logger""log""os""time")type User struct {ID uintName stringEmail *stringAge uint8Birthday *time.TimeMemberNumber sql.NullStringActivatedAt sql.NullTimeCreatedAt time.TimeUpdatedAt time.Time}func main() {// 连接对应的数据库dsn := "root:root@tcp(192.168.193.128:3306)/grom_test?charset=utf8mb4&parseTime=True&loc=Local"newLogger := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer(日志输出的目标,前缀和日志包含的内容——译者注)logger.Config{SlowThreshold: time.Second, // 慢 SQL 阈值LogLevel: logger.Info, // 日志级别IgnoreRecordNotFoundError: true, // 忽略ErrRecordNotFound(记录未找到)错误Colorful: true, // 使用用彩色打印},)db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: newLogger})if err != nil {panic(err)}birthday := time.Now()user := User{Name: "Jinzhu", Age: 18, Birthday: &birthday}result := db.Create(&user) // 通过数据的指针来创建// 1fmt.Println(user.ID) // 返回插入数据的主键// fmt.Println(result.Error) // 返回 error// 1fmt.Println(result.RowsAffected) // 返回插入记录的条数}
日志:
INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('Jinzhu',NULL,18,'2022-05-26 16:34:40.986',NULL,NULL,'2022-05-26 16:34:40.988','2022-05-26 16:34:40.988')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码birthday := time.Now()user := User{Name: "Jinzhu", Age: 18, Birthday: &birthday}db.Select("Name", "Age", "CreatedAt").Create(&user) // 创建记录并更新给出的字段。}
日志:
INSERT INTO `users` (`name`,`age`,`created_at`,`updated_at`) VALUES ('Jinzhu',18,'2022-05-26 16:39:00.906','2022-05-26 16:39:00.906')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码birthday := time.Now()user := User{Name: "Jinzhu", Age: 18, Birthday: &birthday}db.Omit("Name", "Age", "CreatedAt").Create(&user) // 创建一个记录且一同忽略传递给略去的字段值。}
日志:
INSERT INTO `users` (`email`,`birthday`,`member_number`,`activated_at`,`updated_at`) VALUES (NULL,'2022-05-26 16:39:00.904',NULL,NULL,'2022-05-26 16:39:00.906')
GORM 允许用户定义的钩子有 BeforeSave, BeforeCreate, AfterSave, AfterCreate 创建记录时将调用这些钩子方法,请参考 Hooks中关于生命周期的详细信息
func (u *User) BeforeCreate(tx *gorm.DB) (err error) { u.UUID = uuid.New() if u.Role == "admin" { return errors.New("invalid role") } return}
如果想跳过 钩子 方法,可以使用 SkipHooks 会话模式,例如:
DB.Session(&gorm.Session{SkipHooks: true}).Create(&user)DB.Session(&gorm.Session{SkipHooks: true}).Create(&users)DB.Session(&gorm.Session{SkipHooks: true}).CreateInBatches(users, 100)
要有效地插入大量记录,请将一个 slice 传递给 Create 方法。 GORM 将生成单独一条SQL语句来插入所有数据,并回填主键的值,钩子方法也会被调用。
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码var users = []User{{Name: "jinzhu1"}, {Name: "jinzhu2"}, {Name: "jinzhu3"}}db.Create(&users)for _, user := range users {// 5 6 7fmt.Println(user.ID)}}
日志:
INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu1',NULL,0,NULL,NULL,NULL,'2022-05-26 17:09:15.298','2022-05-26 17:09:15.298'),('jinzhu2',NULL,0,NULL,NULL,NULL,'2022-05-26 17:09:15.298','2022-05-26 17:09:15.298'),('jinzhu3',NULL,0,NULL,NULL,NULL,'2022-05-26 17:09:15.298','2022-05-26 17:09:15.298')
SQL语句的长度是有限制的;如果一次性插入的数据量过大,有可能会执行不了;可以采用分批插入;
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码var users = []User{{Name: "jinzhu1"}, {Name: "jinzhu2"}, {Name: "jinzhu3"}}// 每两条插入一次db.CreateInBatches(users, 2)for _, user := range users { // 8 9 10fmt.Println(user.ID)}}
日志:
INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu1',NULL,0,NULL,NULL,NULL,'2022-05-26 17:11:49.606','2022-05-26 17:11:49.606'),('jinzhu2',NULL,0,NULL,NULL,NULL,'2022-05-26 17:11:49.606','2022-05-26 17:11:49.606')INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu3',NULL,0,NULL,NULL,NULL,'2022-05-26 17:11:49.608','2022-05-26 17:11:49.608')
GORM 支持根据 map[string]interface{} 和 []map[string]interface{}{} 创建记录;
注意: 根据 map 创建记录时,association 不会被调用,且主键也不会自动填充
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码db.Model(&User{}).Create(map[string]interface{}{"Name": "jinzhu", "Age": 18,})}
日志:
INSERT INTO `users` (`age`,`name`) VALUES (18,'jinzhu')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码db.Model(&User{}).Create([]map[string]interface{}{{"Name": "jinzhu_1", "Age": 18},{"Name": "jinzhu_2", "Age": 20},})}
日志:
INSERT INTO `users` (`age`,`name`) VALUES (18,'jinzhu_1'),(20,'jinzhu_2')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")type CreditCard struct {gorm.ModelNumber stringUserID uint}type User struct {ID uintName stringEmail *stringAge uint8Birthday *time.TimeMemberNumber sql.NullStringActivatedAt sql.NullTimeCreatedAt time.TimeUpdatedAt time.TimeCreditCard CreditCard}func main() {// 省略连接数据库代码db.Create(&User{Name: "jinzhu",CreditCard: CreditCard{Number: "411111111111"},})}
日志:
INSERT INTO `credit_cards` (`created_at`,`updated_at`,`deleted_at`,`number`,`user_id`) VALUES ('2022-05-26 17:21:24.763','2022-05-26 17:21:24.763',NULL,'411111111111',20) ON DUPLICATE KEY UPDATE `user_id`=VALUES(`user_id`)INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu',NULL,0,NULL,NULL,NULL,'2022-05-26 17:21:24.761','2022-05-26 17:21:24.761')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码db.Omit("CreditCard").Create(&User{Name: "jinzhu"})}
日志:
INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu',NULL,0,NULL,NULL,NULL,'2022-05-26 17:25:34.453','2022-05-26 17:25:34.453')
package mainimport ("database/sql""fmt""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time")// 省略表结构func main() {// 省略连接数据库代码db.Omit(clause.Associations).Create(&User{Name: "jinzhu"})}
日志:
INSERT INTO `users` (`name`,`email`,`age`,`birthday`,`member_number`,`activated_at`,`created_at`,`updated_at`) VALUES ('jinzhu',NULL,0,NULL,NULL,NULL,'2022-05-26 17:27:12.382','2022-05-26 17:27:12.382')
来源地址:https://blog.csdn.net/qq_43135259/article/details/124987453
--结束END--
本文标题: GORM常用的插入
本文链接: https://lsjlt.com/news/380017.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0