• 主表批量维护从表--从表实现


    一. 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);
    }
  • 相关阅读:
    ruby **option作为函数参数,map的key必须是符号
    计算2..n的素数
    css,世界上没有绝对简单的事情
    modernizr的介绍和使用
    图解HTTP
    px、em、rem区别介绍
    web开发规范文档
    iscroll API
    sulime text 常用快捷键总结
    a href="javascript:void(0)"
  • 原文地址:https://www.cnblogs.com/xx0405/p/6816793.html
Copyright © 2020-2023  润新知