From 56814af54fced297a7693c014a26bee53c68678e Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Wed, 12 Jul 2023 12:47:19 +0800 Subject: [PATCH] BaseService#getMapBy* support value mapper --- .../mybatis/base/BaseService.java | 13 ++++++++++++- .../mybatis/base/BaseServiceImpl.java | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java index 90e5215..a7517f0 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseService.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.function.Supplier; public interface BaseService extends IService { @@ -28,6 +29,11 @@ public interface BaseService extends IService { */ Map getMapByIds(Collection ids); + /** + * 根据 id 获取 + */ + Map getMapByIds(Collection ids, Function mapper); + /** * 根据业务主键获取记录 */ @@ -57,6 +63,11 @@ public interface BaseService extends IService { */ Map getMapByBizIds(Collection bizIds); + /** + * 根据业务主键获取 + */ + Map getMapByBizIds(Collection bizIds, Function mapper); + /** * 根据 id 更新,null 会被更新为 null */ @@ -72,4 +83,4 @@ public interface BaseService extends IService { */ boolean updateBatchByIdsWithNull(Collection entityList, int batchSize); -} \ No newline at end of file +} diff --git a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java index 040f316..30465e8 100644 --- a/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java +++ b/starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/base/BaseServiceImpl.java @@ -16,11 +16,13 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ReflectionUtils; +import javax.annotation.Nullable; import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.function.Supplier; /** @@ -29,6 +31,7 @@ import java.util.function.Supplier; */ public class BaseServiceImpl, T extends BaseEntity> extends ServiceImpl implements BaseService, ValidateSupport, InitializingBean { + @Nullable private BizIdColumnField bizIdColumnField; @Override @@ -56,10 +59,15 @@ public class BaseServiceImpl, T extends BaseEntity> exte @Override public Map getMapByIds(Collection ids) { + return getMapByIds(ids, Function.identity()); + } + + @Override + public Map getMapByIds(Collection ids, Function mapper) { if (isEmpty(ids)) { return Collections.emptyMap(); } - return StreamUtils.toMap(super.listByIds(ids), T::getId); + return StreamUtils.toMap(listByIds(ids), T::getId, mapper); } @Override @@ -97,11 +105,16 @@ public class BaseServiceImpl, T extends BaseEntity> exte @Override public Map getMapByBizIds(Collection bizIds) { + return getMapByBizIds(bizIds, Function.identity()); + } + + @Override + public Map getMapByBizIds(Collection bizIds, Function mapper) { assertBidColumnFieldExist(); if (isEmpty(bizIds)) { return Collections.emptyMap(); } - return StreamUtils.toMap(listByBizIds(bizIds), entity -> bizIdColumnField.getValue(entity)); + return StreamUtils.toMap(listByBizIds(bizIds), entity -> bizIdColumnField.getValue(entity), mapper); } @Override @@ -147,4 +160,4 @@ public class BaseServiceImpl, T extends BaseEntity> exte return mapperClass.getName() + StringPool.DOT + "alwaysUpdateSomeColumnById"; } -} \ No newline at end of file +} -- GitLab