背景:日常开发中使用框架常常有字典功能,导出的时候每创建一个字典就创建一个转化器也比较麻烦,所以这里封装一个统一转化器
转化器:
package com.tianyi.nppm.biz.commissioning.dto.supervision.auth.tool;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.util.Map;
/**
* @Author: 翎墨袅
* @Description: 导出通用字典转化器
* @Date Created in 2024-08-22 10:26
* @Modified By:
*/
public class DictionaryConverter implements Converter<String> {
//字典键值对
Map<String,String> dictionaryMap = null;
@Override
public Class<?> supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
/**
*导入触发(v->K)
*/
@Override
public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return getDictionaryMap(contentProperty).get(cellData.getStringValue());
}
/**
* 导出触发(K->V)
*/
@Override
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new WriteCellData<String>(getDictionaryMap(contentProperty).get(value));
}
public Map<String, String> getDictionaryMap(ExcelContentProperty contentProperty) {
if (dictionaryMap==null)
dictionaryMap=DictionaryKV.getDictionaryMap(contentProperty.getField().getDeclaringClass().getName(),contentProperty.getField().getName());
return dictionaryMap;
}
}
字典查询工具类
package com.tianyi.nppm.biz.commissioning.dto.supervision.auth.tool;
import com.tianyi.system.api.dto.SysDictDataDto;
import com.tianyi.system.api.service.ApiDictDataService;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author: 翎墨袅
* @Description: 类属性与字典对应关系
* @Date Created in 2024-08-22 10:35
* @Modified By:
*/
@Component
public class DictionaryKV {
private static DictionaryKV dictionaryKV;
@Resource//这里换成自己项目查询字典值的Service
ApiDictDataService apiDictDataService;
//字段字典对应关系(k:类名.属性,v:字典名)
private static Map<String,String> dictionarys= new HashMap<>(){{
put("com.xxx.类名.属性名1","字典唯一标识1");
put("com.xxx.类名.属性名2","字典唯一标识2");
}};
/**
* @Description: 返回对应字典
* @param className: 类全路径
* @param fieldName: 属性名
* @Return: java.util.Map<java.lang.String,java.lang.String>
* @Author: 翎墨袅
* @Date: 2024/8/22 13:50
*/
public static Map<String,String> getDictionaryMap(String className,String fieldName){
Map<String,String> dictionaryMap;
//查询字典数据(不放在外部第一次查询进行存储,之后直接返回的原因是保证最新字典数据)
List<SysDictDataDto> sysDictDataDtoList = dictionaryKV.apiDictDataService.dictType(dictionarys.get(className+"."+fieldName), new HttpHeaders()).getData();
//根据list构建正反两个方向map,导入导出都要用固双向,这里自己根据业务修改
dictionaryMap=sysDictDataDtoList.stream().collect(Collectors.toMap(SysDictDataDto::getDictLabel, SysDictDataDto::getDictValue));
dictionaryMap.putAll(sysDictDataDtoList.stream().collect(Collectors.toMap(SysDictDataDto::getDictValue, SysDictDataDto::getDictLabel)));
return dictionaryMap;
}
@PostConstruct //初始化
public void init() {
dictionaryKV = this;
dictionaryKV.apiDictDataService = this.apiDictDataService;
}
}