zoukankan      html  css  js  c++  java
  • 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用例

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

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

  • 相关阅读:
    Oracle中的4大空值处理函数用法举例
    PyCharm安装
    Python安装与环境变量的配置
    多层分组排序问题
    将时间点的数据变成时间段的数据
    根据状态变化情况,求最大值和最小值
    ubuntu 源码安装 swig
    CSDN博客排名第一名,何许人也
    thinkPHP的常用配置项
    拔一拔 ExtJS 3.4 里你遇到的没遇到的 BUG(1)
  • 原文地址:https://www.cnblogs.com/zq1003/p/15776469.html
Copyright © 2011-2022 走看看