sql 批量跟新sql 语句


问题:

更新一批数据,根据不同code 更新不同的时间。

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

解决:

mybatis-plus  有批量更新的方法

都是根据ID 更新。

没法解决问题。

写 sql 语句。

 entity层

@Data
public class UpdateOrderDto {
    /**
     * 订单号
     */
    private String orderCode;
    /**
     * 买家支付时间
     */
    private String paymentTime;
}

service层

 

  boolean updateOrderPaymentTime(List updateOrderDto);

impl层

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

mapper层

int updateOrderPaymentTime(@Param("updateOrderDto")  List updateOrderDto);

xml层  重点

 "updateOrderPaymentTime">
        UPDATE  htc.htc_order a JOIN
        (
        <foreach collection="updateOrderDto" item="item" separator="UNION">
            SELECT
            
            "${item.paymentTime}" AS payment_time,
            "${item.orderCode}" AS order_code
        foreach>
        ) b USING (order_code)
        SET a.payment_time = b.payment_time
    

将  传进来的list集合 ,循环查询集合属性取别名,与数据库字段相对应,最后取并集。

通过 USing 的 用法,进行更新操作。妙哉!

test用例

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

明亮教的方法,仅此做记录。