package main import ( "database/sql/driver" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type student struct { Name string Age int } func (u student) Value() (driver.Value, error) { return []interface{}{u.Name, u.Age}, nil } func main() { // 连接数据库 var db *sqlx.DB dsn := "root:123456@(127.0.0.1:3306)/golang?charset=utf8mb4&parseTime=true" db, err := sqlx.Connect("mysql", dsn) if err != nil { fmt.Printf("connect DB failed, err:%v ", err) return } db.SetMaxOpenConns(20) db.SetMaxIdleConns(10) // 批量插入sqlx.In u1 := student{Name: "七米", Age: 18} u2 := student{Name: "q1mi", Age: 28} u3 := student{Name: "小王子", Age: 38} users := []interface{}{u1, u2, u3} query, args, _ := sqlx.In( "INSERT INTO stu (name, age) VALUES (?), (?), (?)", users..., // 如果arg实现了 driver.Valuer, sqlx.In 会通过调用 Value()来展开它 ) fmt.Println(query) // 查看生成的querystring fmt.Println(args) // 查看生成的args _, err1 := db.Exec(query, args...) fmt.Println(err1) }
老师博客:www.liwenzhou.com