重写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>