db.CreateInBatches undefined (type *gorm.DB has no field or method CreateInBatches)
根据官方的代码测试批量插入数据出现以下问题
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
)
type Student struct {
gorm.Model
Username string `gorm:"type:varchar(20);not null " `
Password string `gorm:"type:varchar(500);not null" `
Age int `gorm:"type:int;DEFAULT:18" `
}
func main() {
// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
db, err := gorm.Open("mysql",
fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
"root", "123456", "127.0.0.1", "3306", "ginblog",
))
if err != nil {
fmt.Println("连接数据库出错:", err)
}
db.SingularTable(true)
db.AutoMigrate(&Student{})
// user := Student{Username: "Jinzhu", Age: 18, Password: "123456"}
// result := db.Create(&user) // 通过数据的指针来创建
// fmt.Println(result)
var students = []Student{{Username: "jinzhu5", Password: "123456"}, {Username: "jinzhu2", Password: "123456"}, {Username: "jinzhu3", Password: "123456"}}
db.CreateInBatches(&students, 1)
// for _, student := range students {
// student.ID //
// }
}
官方第一种方法
.\demo01.go:34:11: too many arguments in call to db.Create
have (*[]Student, number)
want (interface {})
报错
官方第二种方法
db.CreateInBatches undefined (type *gorm.DB has no field or method CreateInBatches)
解决方案
还在研究中~~~