增加List<KeyValue>转换为Map的接口

This commit is contained in:
mazhicheng 2020-01-16 22:07:31 +08:00
parent 6ba18840d1
commit 79a23853fe
4 changed files with 31 additions and 0 deletions

View File

@ -184,6 +184,14 @@ public interface BaseService<T> {
*/ */
List<KeyValue> getKeyValueList(Wrapper queryWrapper); List<KeyValue> getKeyValueList(Wrapper queryWrapper);
/***
* 获取键值对的Map
*
* @param queryWrapper
* @return
*/
Map<String, Object> getKeyValueMap(Wrapper queryWrapper);
/** /**
* 获取View Object对象 * 获取View Object对象
* @param id 主键 * @param id 主键

View File

@ -242,6 +242,12 @@ public class BaseServiceImpl<M extends BaseCrudMapper<T>, T> extends ServiceImpl
return keyValueList; return keyValueList;
} }
@Override
public Map<String, Object> getKeyValueMap(Wrapper queryWrapper) {
List<KeyValue> keyValueList = getKeyValueList(queryWrapper);
return BeanUtils.convertKeyValueList2Map(keyValueList);
}
@Override @Override
public FieldBinder<T> bindingFieldTo(List voList){ public FieldBinder<T> bindingFieldTo(List voList){
return new FieldBinder<>(this, voList); return new FieldBinder<>(this, voList);

View File

@ -3,6 +3,7 @@ package com.diboot.core.util;
import com.diboot.core.config.Cons; import com.diboot.core.config.Cons;
import com.diboot.core.entity.BaseEntity; import com.diboot.core.entity.BaseEntity;
import com.diboot.core.vo.KeyValue;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
@ -650,6 +651,18 @@ public class BeanUtils {
return null; return null;
} }
/**
* 转换keyValue集合为Map
* @param keyValueList
* @return
*/
public static Map<String, Object> convertKeyValueList2Map(List<KeyValue> keyValueList) {
if(V.notEmpty(keyValueList)){
return keyValueList.stream().collect(Collectors.toMap(KeyValue::getK, KeyValue::getV));
}
return Collections.EMPTY_MAP;
}
/** /**
* 根据指定Key对list去重 * 根据指定Key对list去重
* @param list * @param list
@ -689,4 +702,5 @@ public class BeanUtils {
} }
return lambda; return lambda;
} }
} }

View File

@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* BaseService接口实现测试 (需先执行example中的初始化SQL) * BaseService接口实现测试 (需先执行example中的初始化SQL)
@ -139,6 +140,8 @@ public class BaseServiceTest {
List<KeyValue> keyValues = dictionaryService.getKeyValueList("GENDER"); List<KeyValue> keyValues = dictionaryService.getKeyValueList("GENDER");
Assert.assertTrue(keyValues.size() == 2); Assert.assertTrue(keyValues.size() == 2);
Assert.assertTrue(keyValues.get(0).getV().equals("M")); Assert.assertTrue(keyValues.get(0).getV().equals("M"));
Map<String, Object> kvMap = BeanUtils.convertKeyValueList2Map(keyValues);
Assert.assertTrue(kvMap.get("").equals("F"));
} }
@Test @Test