通過(guò)預(yù)加載可以實(shí)現(xiàn)各個(gè)模型之間的一對(duì)多關(guān)系,例如下面的代碼,其中device結(jié)構(gòu)體對(duì)應(yīng)多個(gè)DeviceModular,DeviceModular又有多個(gè)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()
}