一. mapper 批量维护实现:
<!-- 根据 Bid 批量新增 --> <insert id="batchInsertByBid"> insert into t_quote_( quote_type_,bid_id_,tender_id_,create_by_, attribute_name_,product_name_,product_type_,create_time_ ) values <foreach collection="quoteList" item="item" separator=","> (1,#{bid.id},null,#{bid.createBy},#{item.attributeName},#{item.productName},#{item.productType},now()) </foreach> </insert> <!-- 批量修改 --> <update id="batchUpdateByList" parameterType="java.util.List"> <foreach collection="quoteList" item="item" index="index" open="" close="" separator=";"> update t_quote_ <trim prefix="set" suffixOverrides=","> <if test='item.attributeName != null'>attribute_name_=#{item.attributeName,jdbcType=VARCHAR},</if> <if test='item.productName != null'>product_name_=#{item.productName,jdbcType=VARCHAR},</if> <if test='item.productType != null'>product_type_=#{item.productType,jdbcType=VARCHAR},</if> </trim> where id_ = #{item.id} </foreach> </update> <!-- 根据 Bid 批量删除 --> <delete id="batchDeleteByBidIdAndIdNotIn"> delete from t_quote_ where quote_type_ = 1 and bid_id_ = #{bidId} <if test="null != quoteList and quoteList.size > 0"> and id_ not in <foreach collection="quoteList" item="item" open="(" separator="," close=")"> #{item.id} </foreach> </if> </delete> <!-- 根据 Bid 批量全部删除 --> <delete id="batchDeleteByBidIdAndIdNotIn"> delete from t_quote_ where quote_type_ = 1 and bid_id_ = #{bidId} </delete>
二. 接口层批量增删改申明
/** * 根据 Bid 批量新增 * * @param quoteList * @param bid * @return */ public int batchInsertByBid(@Param("quoteList") List<Quote> quoteList, @Param("bid") Bid bid); /** * 批量修改 * @param quoteList * @return */ public int batchUpdateByList(@Param("quoteList") List<Quote> quoteList); /** * 根据 Bid 批量删除 * @param quoteList * @param bidId * @return */ public int batchDeleteByBidIdAndIdNotIn(@Param("quoteList") List<Quote> quoteList, @Param("bidId") Long bidId); /** * 根据 Bid 批量全部删除 * @param bidId * @return */ public int batchDeleteByBidId(@Param("bidId") Long bidId);
三. 接口层批量维护实现,同时出现可能增删改的业务 (JDK8可以直接在 接口中写实现方法,用default申明即可)
/** * 通过暴力方式 <全部删除再全部新增> 批量维护 * @param quoteList * @param bid */ public default void updateByBid(List<Quote> quoteList, Bid bid){ batchDeleteByBidId(bid.getId()); if(quoteList.size() > 0) batchInsertByBid(quoteList, bid); } /** * 通过对比方式 <全部删除再全部新增> 批量维护 * @param updQuoteList * @param addQuoteList * @param bid */ public default void updateByBid(List<Quote> updQuoteList, List<Quote> addQuoteList, Bid bid){ batchDeleteByBidIdAndIdNotIn(bid.getId(), updQuoteList); if(updQuoteList.size() > 0) batchUpdateByList(updQuoteList); if(addQuoteList.size() > 0) batchInsertByBid(addQuoteList, bid); }