From 79a23853fe956741d594442afad92b98ad2c07b3 Mon Sep 17 00:00:00 2001 From: mazhicheng Date: Thu, 16 Jan 2020 22:07:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0List=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E4=B8=BAMap=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/diboot/core/service/BaseService.java | 8 ++++++++ .../diboot/core/service/impl/BaseServiceImpl.java | 6 ++++++ .../main/java/com/diboot/core/util/BeanUtils.java | 14 ++++++++++++++ .../diboot/core/test/service/BaseServiceTest.java | 3 +++ 4 files changed, 31 insertions(+) diff --git a/diboot-core/src/main/java/com/diboot/core/service/BaseService.java b/diboot-core/src/main/java/com/diboot/core/service/BaseService.java index f128eee..ff37d01 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/BaseService.java +++ b/diboot-core/src/main/java/com/diboot/core/service/BaseService.java @@ -184,6 +184,14 @@ public interface BaseService { */ List getKeyValueList(Wrapper queryWrapper); + /*** + * 获取键值对的Map + * + * @param queryWrapper + * @return + */ + Map getKeyValueMap(Wrapper queryWrapper); + /** * 获取View Object对象 * @param id 主键 diff --git a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java index 424f134..d1c7852 100644 --- a/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java +++ b/diboot-core/src/main/java/com/diboot/core/service/impl/BaseServiceImpl.java @@ -242,6 +242,12 @@ public class BaseServiceImpl, T> extends ServiceImpl return keyValueList; } + @Override + public Map getKeyValueMap(Wrapper queryWrapper) { + List keyValueList = getKeyValueList(queryWrapper); + return BeanUtils.convertKeyValueList2Map(keyValueList); + } + @Override public FieldBinder bindingFieldTo(List voList){ return new FieldBinder<>(this, voList); diff --git a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java index 3d5babc..2884477 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java +++ b/diboot-core/src/main/java/com/diboot/core/util/BeanUtils.java @@ -3,6 +3,7 @@ package com.diboot.core.util; import com.diboot.core.config.Cons; import com.diboot.core.entity.BaseEntity; +import com.diboot.core.vo.KeyValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.support.AopUtils; @@ -650,6 +651,18 @@ public class BeanUtils { return null; } + /** + * 转换keyValue集合为Map + * @param keyValueList + * @return + */ + public static Map convertKeyValueList2Map(List keyValueList) { + if(V.notEmpty(keyValueList)){ + return keyValueList.stream().collect(Collectors.toMap(KeyValue::getK, KeyValue::getV)); + } + return Collections.EMPTY_MAP; + } + /** * 根据指定Key对list去重 * @param list @@ -689,4 +702,5 @@ public class BeanUtils { } return lambda; } + } diff --git a/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java b/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java index f00dcaa..cbd1ae0 100644 --- a/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java +++ b/diboot-core/src/test/java/diboot/core/test/service/BaseServiceTest.java @@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; /** * BaseService接口实现测试 (需先执行example中的初始化SQL) @@ -139,6 +140,8 @@ public class BaseServiceTest { List keyValues = dictionaryService.getKeyValueList("GENDER"); Assert.assertTrue(keyValues.size() == 2); Assert.assertTrue(keyValues.get(0).getV().equals("M")); + Map kvMap = BeanUtils.convertKeyValueList2Map(keyValues); + Assert.assertTrue(kvMap.get("女").equals("F")); } @Test