From cea131c5e5eda0a83357d9846f0d2441f7da6fca Mon Sep 17 00:00:00 2001 From: zk Date: Tue, 10 Mar 2026 20:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=89=B9=E9=87=8F=E6=8F=92?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-local.yml | 6 -- .../org/jiayunet/mapper/CommonMapper.java | 100 +++++++++++++----- 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/client-api/src/main/resources/application-local.yml b/client-api/src/main/resources/application-local.yml index 4242c74..9b5c602 100644 --- a/client-api/src/main/resources/application-local.yml +++ b/client-api/src/main/resources/application-local.yml @@ -40,12 +40,6 @@ app: #登陆配置 login: - # 登陆图片验证相关 - login_image_verification: - enable: false - images_code_sources: 123456789ABCDEFGHJKLMNPQRSTUVWXYZ - images_w: 111 - images_h: 36 # token配置 token: exceed_time: 129600 diff --git a/common/src/main/java/org/jiayunet/mapper/CommonMapper.java b/common/src/main/java/org/jiayunet/mapper/CommonMapper.java index 6abd8d7..9fd3ee4 100644 --- a/common/src/main/java/org/jiayunet/mapper/CommonMapper.java +++ b/common/src/main/java/org/jiayunet/mapper/CommonMapper.java @@ -6,64 +6,108 @@ import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; /** + * 通用Mapper接口,提供批量操作功能 + * * @author zk */ public interface CommonMapper extends BaseMapper { + /** + * 默认批量操作大小 + */ int DEFAULT_BATCH_SIZE = 5000; + /** + * 批量插入部分字段(需在XML中实现) + * + * @param entityList 实体列表 + * @return 插入成功的记录数 + */ int insertBatchSomeColumn(Collection entityList); + /** + * 批量更新(需在XML中实现) + * + * @param entityList 实体列表 + * @return 更新成功的记录数 + */ int updateBatchMethod(Collection entityList); - + /** + * 批量插入(使用默认批次大小) + * + * @param entityList 实体列表 + * @return 插入成功的记录数 + */ default int batchInsert(List entityList) { return this.batchInsert(entityList, DEFAULT_BATCH_SIZE); } + /** + * 批量插入(自定义批次大小) + * + * @param entityList 实体列表 + * @param batchSize 每批次大小 + * @return 插入成功的记录数 + */ default int batchInsert(List entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { return 0; - } else { - if (batchSize <= 0) { - batchSize = 5000; - } - - List> partition = partition(entityList, batchSize); - - AtomicInteger total = new AtomicInteger(); - partition.forEach((e) -> { - total.addAndGet(this.insertBatchSomeColumn(e)); - }); - return total.get(); } + if (batchSize <= 0) { + batchSize = DEFAULT_BATCH_SIZE; + } + + List> partition = partition(entityList, batchSize); + int total = 0; + for (List batch : partition) { + total += this.insertBatchSomeColumn(batch); + } + return total; } + /** + * 批量更新(使用默认批次大小) + * + * @param entityList 实体列表 + * @return 更新成功的记录数 + */ default int batchUpdate(List entityList) { - return this.batchUpdate(entityList, 5000); + return this.batchUpdate(entityList, DEFAULT_BATCH_SIZE); } + /** + * 批量更新(自定义批次大小) + * + * @param entityList 实体列表 + * @param batchSize 每批次大小 + * @return 更新成功的记录数 + */ default int batchUpdate(List entityList, int batchSize) { if (CollectionUtils.isEmpty(entityList)) { return 0; - } else { - if (batchSize <= 0) { - batchSize = 5000; - } - - List> partition = this.partition(entityList, batchSize); - AtomicInteger total = new AtomicInteger(); - partition.forEach((e) -> { - total.addAndGet(updateBatchMethod(e)); - }); - return total.get(); } + if (batchSize <= 0) { + batchSize = DEFAULT_BATCH_SIZE; + } + + List> partition = partition(entityList, batchSize); + int total = 0; + for (List batch : partition) { + total += updateBatchMethod(batch); + } + return total; } - - default List> partition(List entityList, Integer batchSize) { + /** + * 将列表分批 + * + * @param entityList 实体列表 + * @param batchSize 每批次大小 + * @return 分批后的列表 + */ + default List> partition(List entityList, int batchSize) { List> partition = new ArrayList<>(); for (int i = 0; i < entityList.size(); i += batchSize) {