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