通过预加载可以实现各个模型之间的一对多关系,例如下面的代码,其中device结构体对应多个DeviceModular,DeviceModular又有多个CommWeimaqi
tx := db.SqlDB.Begin()
sqlStr := "INSERT INTO report_form (id,create_time,choose_count, device_fall_count,game_order_count,coin_count,member_count," +
"day_member_count,visit_count,lgz_coin_count,weimaqi_coin_count,store_id,real_coin_count,m_coin_count,coin_spec) VALUES "
vals := []interface{}{}
const rowSQL = "(?,?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?)"
var inserts []string
for _, elem := range reportForms {
inserts = append(inserts, rowSQL)
vals = append(vals, elem.ID, elem.CreateTime, elem.ChooseCount, elem.DeviceFallCount, elem.GameOrderCount, elem.CoinCount, elem.MemberCount, elem.DayMemberCount, elem.VisitCount, elem.LgzCoinCount, elem.WeimaqiCoinCount, elem.StoreId, elem.RealCoinCount, elem.MCoinCount, elem.CoinSpec)
}
sqlStr = sqlStr + strings.Join(inserts, ",")
err := tx.Exec(sqlStr, vals...).Error
if err != nil {
tx.Rollback()
fmt.Print(err)
}else {
tx.Commit()
}