重写mybatis-plus的saveUpdate方法

1.问题出现

同步外部数据的时候,如果需要同步逻辑删除的数据,mybatis-plus的saveOrUpdate||saveOrUpdateBath方法底层根据先查出数据数据是否存在,存在则更新不存在则新增,数据逻辑删除时,mybatis-plus查询不出来会执行插入造成主键冲突异常

2.问题解决(重写方法)

Mapper.java

public interface MdmCustomerBankInfoMapper extends BaseMapper<MdmCustomerBankInfo> {
int saveOrUpdate(MdmCustomerBankInfo list);
int saveOrUpdateBatch(List<MdmCustomerBankInfo> list);
}

Mapper.xml

saveOrUpdate

    <update id="selfSaveOrUpdateBatch">
INSERT INTO mdm_customer_bank_info (id,customer_id,account_holder_name,bank_country,bank_account_num,bank_name,
bank_branch_code,created_by,created_time,updated_by,updated_time,is_deleted) SELECT * FROM (
SELECT
#{item.id} as id,
#{item.customerId} as customer_id,
#{item.accountHolderName} as account_holder_name,
#{item.bankCountry} as bank_country,
#{item.bankAccountNum} as bank_account_num,
#{item.bankName} as bank_name,
#{item.bankBranchCode} as bank_branch_code,
#{item.createdBy} as created_by,
#{item.createdTime} as created_time,
#{item.updatedBy} as updated_by,
#{item.updatedTime} as updated_time,
#{item.isDeleted} as is_deleted
FROM DUAL
) t2
on DUPLICATE KEY UPDATE customer_id=t2.customer_id,account_holder_name=t2.account_holder_name,bank_country=t2.bank_country,bank_account_num=t2.bank_account_num,
bank_name=t2.bank_name,bank_branch_code=t2.bank_branch_code,
created_by=t2.created_by,created_time=t2.created_time,updated_by=t2.updated_by,updated_time=t2.updated_time,is_deleted = t2.is_deleted
</update>

saveOrUpdateBath

    <update id="selfSaveOrUpdateBatch">
INSERT INTO mdm_customer_bank_info (id,customer_id,account_holder_name,bank_country,bank_account_num,bank_name,
bank_branch_code,created_by,created_time,updated_by,updated_time,is_deleted) SELECT * FROM (
<foreach collection="list" item="item" index="index" separator="union">
SELECT
#{item.id} as id,
#{item.customerId} as customer_id,
#{item.accountHolderName} as account_holder_name,
#{item.bankCountry} as bank_country,
#{item.bankAccountNum} as bank_account_num,
#{item.bankName} as bank_name,
#{item.bankBranchCode} as bank_branch_code,
#{item.createdBy} as created_by,
#{item.createdTime} as created_time,
#{item.updatedBy} as updated_by,
#{item.updatedTime} as updated_time,
#{item.isDeleted} as is_deleted
FROM DUAL
</foreach>
) t2
on DUPLICATE KEY UPDATE customer_id=t2.customer_id,account_holder_name=t2.account_holder_name,bank_country=t2.bank_country,bank_account_num=t2.bank_account_num,
bank_name=t2.bank_name,bank_branch_code=t2.bank_branch_code,
created_by=t2.created_by,created_time=t2.created_time,updated_by=t2.updated_by,updated_time=t2.updated_time,is_deleted = t2.is_deleted
</update>

最新文章

  1. 使用GRUB 添加新的启动项 (menu entry)
  2. MySQL数据表range分区例子
  3. 这种代码怎么改写?以致于在下次增加CustomsType时,不需要再加 if 语句。
  4. 【BZOJ-2460&amp;3105】元素&amp;新Nim游戏 动态维护线性基 + 贪心
  5. MD5 (摘要加密)
  6. php 分词 —— PHPAnalysis无组件分词系统
  7. Quick Sort
  8. sql 泡沫 或者 递归查询
  9. Delphi 为什么它提示PCHAR是不安全的类型呢 Unsafe type &#39;PChar&#39;
  10. envi中多波段图层叠加layer stacking
  11. 【HDOJ】4775 Infinite Go
  12. oracle强化练习之分组函数
  13. Python 接口测试(四)
  14. python打包
  15. Python3.7版本unittest框架添加用例的方法
  16. linux安装Subversion版本控制工具(Subversion + Apache + jsvnadmin)
  17. vector、deque、stack、queue、list以及set的使用
  18. Java基础知识中的注意事项
  19. Y460 安装ubuntu 12.04系统黑屏,登录界面黑屏
  20. python(40):利用utf-8编码判断中文英文字符

热门文章

  1. wsl2 的安装与使用
  2. 查看树莓派CPU当前工作频率
  3. Mint UI中的坑:datetime-picker在PC浏览器上可以显示弹出的日期选择,但是在手机上是空白
  4. Outlook配置文件位置
  5. UI基础 - UIAppearance协议
  6. Keil51单片机解决数字显示不稳的问题
  7. js 浮点数加、减、乘、除。
  8. koa源代码解析
  9. CF1548B Integers Have Friends
  10. editorconfig配置