easyexcel字典通用转化器

背景:日常开发中使用框架常常有字典功能,导出的时候每创建一个字典就创建一个转化器也比较麻烦,所以这里封装一个统一转化器

转化器:

复制代码
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;
    }
}

本来想过这样可能会遇到多个对象情况下同一字典会多次查询的可能性,但测试发现并没有这个问题。

但已经先一步想到解决办法,使用map<sessionID,map<字典名,字典>>进行存储字典信息,使用切面方法完成后触发map删除即可。不过没遇到,便只记录下想法吧

相关推荐
晚霞的不甘7 小时前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
SunnyDays10117 小时前
使用 Java 冻结 Excel 行和列:完整指南
java·冻结excel行和列
摇滚侠7 小时前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
云姜.7 小时前
java多态
java·开发语言·c++
李堇7 小时前
android滚动列表VerticalRollingTextView
android·java
泉-java8 小时前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言
zfoo-framework8 小时前
帧同步和状态同步
java
charlotte102410248 小时前
高并发:关于在等待学校教务系统选课时的碎碎念
java·运维·网络
亓才孓8 小时前
[JDBC]PreparedStatement替代Statement
java·数据库
_F_y9 小时前
C++重点知识总结
java·jvm·c++