3 changed files with 111 additions and 0 deletions
@ -0,0 +1,87 @@
|
||||
package com.biutag.supervision.repository.base; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction; |
||||
import com.google.common.collect.Lists; |
||||
import org.springframework.dao.DuplicateKeyException; |
||||
|
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @ClassName BaseDAO |
||||
* @Description 资源层基础 |
||||
* @Author shihao |
||||
* @Date 2025/12/18 23:30 |
||||
*/ |
||||
public abstract class BaseDAO { |
||||
|
||||
/** |
||||
* 整合单个和批量 |
||||
* @param single |
||||
* @param batch |
||||
* @param wrapper |
||||
* @param function |
||||
* @param <T> |
||||
* @param <E> |
||||
*/ |
||||
protected <T, E> void setBatchQuery(T single, Set<T> batch, LambdaQueryWrapper<E> wrapper, SFunction<E, T> function) { |
||||
if (CollectionUtils.isEmpty(batch)) { |
||||
if (isEmpty(single)) { |
||||
return; |
||||
} |
||||
wrapper.eq(function, single); |
||||
} else { |
||||
if (isEmpty(single)) { |
||||
if (batch.size() == 1) { |
||||
wrapper.eq(function, batch.iterator().next()); |
||||
} else { |
||||
wrapper.in(function, batch); |
||||
} |
||||
} else { |
||||
Set<T> merge = new HashSet<>(batch); |
||||
merge.add(single); |
||||
wrapper.in(function, merge); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 批量添加 20一组 |
||||
* @return |
||||
* @param <T> |
||||
*/ |
||||
protected <T> void innerBatchInsert(HBaseMapper<T> mapper, List<T> entities, String message) { |
||||
try { |
||||
if (CollectionUtils.isEmpty(entities)) { |
||||
return; |
||||
} |
||||
int i; |
||||
if (entities.size() == 1) { |
||||
i = mapper.insert(entities.get(0)); |
||||
} else { |
||||
i = Lists.partition(entities, 20).stream().map(mapper::insertBatch).reduce(0, Integer::sum); |
||||
} |
||||
if (i != entities.size()) { |
||||
throw new RuntimeException("批量添加失败,预期:" + entities.size() + ",实际插入:" + i); |
||||
} |
||||
} catch (DuplicateKeyException e) { |
||||
throw new RuntimeException("数据已存在"); |
||||
} |
||||
} |
||||
|
||||
protected <T> boolean isEmpty(T data) { |
||||
if (data == null) { |
||||
return true; |
||||
} |
||||
if (data instanceof String) { |
||||
return "".equals(data); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,17 @@
|
||||
package com.biutag.supervision.repository.base; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @ClassName HBaseMpper |
||||
* @Description 批量 |
||||
* @Author shihao |
||||
* @Date 2025/12/18 23:46 |
||||
*/ |
||||
public interface HBaseMapper<T> extends BaseMapper<T> { |
||||
|
||||
int insertBatch(List<T> entities); |
||||
|
||||
} |
||||
Loading…
Reference in new issue