From afe5e183e04a3f5256e5961b4cc32062a0a2003b Mon Sep 17 00:00:00 2001 From: buaixuexideshitongxue <2936013465@qq.com> Date: Thu, 18 Dec 2025 23:53:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=B1=82=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++ .../supervision/repository/base/BaseDAO.java | 87 +++++++++++++++++++ .../repository/base/HBaseMapper.java | 17 ++++ 3 files changed, 111 insertions(+) create mode 100644 src/main/java/com/biutag/supervision/repository/base/BaseDAO.java create mode 100644 src/main/java/com/biutag/supervision/repository/base/HBaseMapper.java diff --git a/pom.xml b/pom.xml index 56fc046..b898501 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,13 @@ + + com.google.guava + guava + 32.1.2-jre + + + org.springframework.boot spring-boot-starter-web diff --git a/src/main/java/com/biutag/supervision/repository/base/BaseDAO.java b/src/main/java/com/biutag/supervision/repository/base/BaseDAO.java new file mode 100644 index 0000000..c750b96 --- /dev/null +++ b/src/main/java/com/biutag/supervision/repository/base/BaseDAO.java @@ -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 + * @param + */ + protected void setBatchQuery(T single, Set batch, LambdaQueryWrapper wrapper, SFunction 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 merge = new HashSet<>(batch); + merge.add(single); + wrapper.in(function, merge); + } + } + } + + + /** + * 批量添加 20一组 + * @return + * @param + */ + protected void innerBatchInsert(HBaseMapper mapper, List 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 boolean isEmpty(T data) { + if (data == null) { + return true; + } + if (data instanceof String) { + return "".equals(data); + } + return false; + } + + +} diff --git a/src/main/java/com/biutag/supervision/repository/base/HBaseMapper.java b/src/main/java/com/biutag/supervision/repository/base/HBaseMapper.java new file mode 100644 index 0000000..7200ea8 --- /dev/null +++ b/src/main/java/com/biutag/supervision/repository/base/HBaseMapper.java @@ -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 extends BaseMapper { + + int insertBatch(List entities); + +}