問題:
更新一批數據,根據不同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用例
說的不是很清楚,理解尚淺,以后深度理解,在做分析。
明亮教的方法,僅此做記錄。