sql 批量跟新sql 語句


 

問題:

更新一批數據,根據不同code 更新不同的時間。

code 不是主鍵 。但是也是唯一的。

 

解決:

mybatis-plus  有批量更新的方法

 

 

 

都是根據ID 更新。

沒法解決問題。

 

寫 sql 語句。

 

 entity層

@Data
public class UpdateOrderDto {
    /**
     * 訂單號
     */
    private String orderCode;
    /**
     * 買家支付時間
     */
    private String paymentTime;
}

 

 

service層

 

 

 

  boolean updateOrderPaymentTime(List<UpdateOrderDto> updateOrderDto);

 

 

impl層

 

 

 

 @Override
    public boolean updateOrderPaymentTime(List<UpdateOrderDto> updateOrderDto) {
        orderMapper.updateOrderPaymentTime(updateOrderDto);
        return true;
    }

 

mapper層

int updateOrderPaymentTime(@Param("updateOrderDto")  List<UpdateOrderDto> updateOrderDto);

 

 

xml層  重點

 

 

 <update id="updateOrderPaymentTime">
        UPDATE  htc.htc_order a JOIN
        (
        <foreach collection="updateOrderDto" item="item" separator="UNION">
            SELECT
            <!--使用 ${} shardingsphere官方問題 詳細參考 github issues:https://github.com/apache/shardingsphere/issues/8108-->
            "${item.paymentTime}" AS payment_time,
            "${item.orderCode}" AS order_code
        </foreach>
        ) b USING (order_code)
        SET a.payment_time = b.payment_time
    </update>

 

將  傳進來的list集合 ,循環查詢集合屬性取別名,與數據庫字段相對應,最后取並集。

 

通過 USing 的 用法,進行更新操作。妙哉!

 

test用例

 

 

說的不是很清楚,理解尚淺,以后深度理解,在做分析。

 

明亮教的方法,僅此做記錄。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM