【Mybatis Plus】自定义组合关系重写 SaveOrUpdateBatch方法

【Mybatis Plus】自定义组合关系重写 SaveOrUpdateBatch方法

主要使用 MybatisPlus 底层方法 SqlHelper.saveOrUpdateBatch

@Transactional(rollbackFor = Exception.class)
public boolean customSaveOrUpdateBatch(Collection<AiUserMarkActionEntity> entityList, int batchSize) {
    TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
    return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize,
        (sqlSession, entity) -> {
            // 自定义判断记录是否存在的逻辑
            LambdaQueryWrapper<AiUserMarkActionEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AiUserMarkActionEntity::getUserId, entity.getUserId())
                        .eq(AiUserMarkActionEntity::getActionType, entity.getActionType())
                        .eq(AiUserMarkActionEntity::getRecordId, entity.getRecordId());
            
            Map<String, Object> param = new HashMap<>();
            param.put(Constants.WRAPPER, queryWrapper);
            
            return CollUtil.isEmpty(sqlSession.selectList(sqlStatement(SqlMethod.SELECT_LIST), param));
        },
        (sqlSession, entity) -> {
            // 自定义更新逻辑
            Map<String, Object> param = new HashMap<>();
            param.put(Constants.ENTITY, entity);
            
            // 设置更新的条件
            UpdateWrapper<AiUserMarkActionEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id", entity.getUserId())
                         .eq("action_type", entity.getActionType())
                         .eq("record_id", entity.getRecordId());
            param.put(Constants.WRAPPER, updateWrapper);
            
            // 执行更新操作
            sqlSession.update(sqlStatement(SqlMethod.UPDATE), param);
        }
    );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值