mybatis的批量保存不用,非要循环去保存,一条sql变N条,作死
public void save( Long bannerId, List<BannerAddress> bannerAddresss) { if (!NumberHelper.greaterZero(bannerId)) { throw new BannerIdIsNullError(); } delete(bannerId); for(BannerAddress bannerAddress bannerAddresss){ dqi.Save(bannerAddress ); } } public void save( Long bannerId, List<BannerAddress> bannerAddresss) { if (!NumberHelper.greaterZero(bannerId)) { throw new BannerIdIsNullError(); } delete(bannerId); List<BannerAddress> saves = CollectionHelper.toList(bannerAddresss); if (saves.isEmpty()) { return; } saves.stream().forEach(e -> e.setBannerId(bannerId)); dqi.batchSave(saves); }
<insert id="batchSave" parameterType="list">
REPLACE INTO <include refid="main_table" />
( `provinceId`, `cityId`, `areaId`, `bannerId`,`provinceName`, `cityName`, `areaName`)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.provinceId}, #{item.cityId}, #{item.areaId}, #{item.bannerId},#{item.provinceName}, #{item.cityName}, #{item.areaName})
</foreach>
</insert>
小细节 REPLACE INTO 重复就做更新操作