静态字典的优雅解决方案 springboot 字典接口实现 java 加强静态字典的实现

项目场景

我们在所有的项目中都会使用到字典这个功能,字典其实又分静态字典和动态字典,顾名思义静态是固定格式和字典,动态字典可能是查出数据库组合的字典内容,我在这里对静态字典做了的研究,对静态字典做了分类加强,以下就是我的代码


代码

字典接口类

java 复制代码
public interface IDict {
    Map<String,String> map();
}

字典实现类一

java 复制代码
@Getter
public enum PriorityEnum implements IDict {
    // 1高 5中 9低
    HIGHT("1", "高"),
    MID("5", "中"),
    LOW("9", "低"),
    ;

    private String key;

    private String value;

    PriorityEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public Map<String, String> map() {
        PriorityEnum[] values = values();
        Map<String, String> map = new LinkedHashMap<>();
        for (PriorityEnum e : values) {
            map.put(e.getKey(), e.getValue());
        }
        return map;
    }
}

字典实现类二

java 复制代码
@Getter
public enum SchedulingStrategyEnum implements IDict {
    MANUAL("0", "手动策略"),
    CYCLE("1", "周期执行"),
    ;

    private String key;

    private String value;

    SchedulingStrategyEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public Map<String, String> map() {
        SchedulingStrategyEnum[] values = values();
        Map<String, String> map = new LinkedHashMap<>();
        for (SchedulingStrategyEnum e : values) {
            map.put(e.getKey(), e.getValue());
        }
        return map;
    }
}

静态字典枚举类

java 复制代码
/**
 * 字典枚举
 *
 */
@Getter
public enum DictEunm {
    PRIORITY("priorityDict", "优先级", PriorityEnum.class, Arrays.asList("/all/cube/rpa")),
    SCHEDULING_STRATEGY("schedulingStrategyDict", "调度策略", SchedulingStrategyEnum.class, Arrays.asList("/all/cube/rpa")),
    BUSINESS_PARAMS_TYPE("businessParamsTypeDict", "业务参数类型", BusinessParamsTypeEnum.class, Arrays.asList("/all/cube/rpa")),
    BUSINESS_PARAMS_COLUMN("businessParamsColumnDict", "业务参数类型所需字段(后端辅助)", BusinessParamsColumnEnum.class, Arrays.asList("/all/cube/rpa")),
    // BUSINESS_PARAMS_TYPE_TRUCTURE("businessParamsTypeStructureDict","业务参数类型对应的操作页面对象", BusinessParamsTypeStructureEnum.class, Arrays.asList("/all/cube/rpa")),
    DATA_TABLE_TYPE("dataTableTypeDict", "数据同步枚举类型", RpaDataTableTypeEnum.class, Arrays.asList("/all/cube/rpa")),


    ;

    private String model;
    private String desc;
    private Class clazz;
    private List<String> path;


    DictEunm(String model, String desc, Class clazz, List<String> path) {
        this.model = model;
        this.desc = desc;
        this.clazz = clazz;
        this.path = path;
    }
}

静态字典配置类

java 复制代码
@Configuration
public class StaticDictConfig {

    @Bean("staticDict")
    public StaticDict staticDict() {
        DictEunm[] values = DictEunm.values();
        Map<String, DictDetailModel> modelMap = new HashMap<>();
        Map<String, List<DictDetailModel>> pathMap = new HashMap<>();
        if (values.length > 0) {
            for (DictEunm dictEunm : values) {
                String model = dictEunm.getModel();
                List<String> path = dictEunm.getPath();
                String desc = dictEunm.getDesc();
                Class clazz = dictEunm.getClazz();
                boolean anEnum = clazz.isEnum();
                Map<String, String> map = new HashMap<>();
                if (anEnum) {
                    Object[] enumConstants = clazz.getEnumConstants();
                    map = ((IDict) enumConstants[0]).map();

                }
                DictDetailModel dictDetailModel = new DictDetailModel(model, desc, path, map);
                if (modelMap.containsKey(model)) {
                    throw new RuntimeException("");
                } else {
                    modelMap.put(model, dictDetailModel);
                }
                for (String s : path) {
                    List<DictDetailModel> orDefault = pathMap.getOrDefault(s, new ArrayList<>());
                    orDefault.add(dictDetailModel);
                    pathMap.put(s, orDefault);
                }

            }
        }
        return new StaticDict(modelMap, pathMap);
    }
}

字典业务层接口

java 复制代码
public interface IDictService {
    DictModel byModelOrPath(DictReqVO dictReqVO);
}

字典业务层实现类

java 复制代码
@Slf4j
@Service
public class DictServiceImpl implements IDictService {
    @Autowired
    private StaticDict staticDict;

    @Override
    public DictModel byModelOrPath(DictReqVO dictReqVO) {
        checkDictReqVO(dictReqVO);
        DictModel res = new DictModel();
        res.setDr(false);
        res.setModel(dictReqVO.getModel());
        res.setPath(dictReqVO.getPath());
        if (StringUtils.isNotBlank(dictReqVO.getModel())) {
            DictDetailModel byModel = staticDict.getByModel(dictReqVO.getModel());
            if (byModel != null) {
                List<DictDetailModel> list = new ArrayList<>();
                list.add(byModel);
                res.setDictionarys(list);
                res.setDr(true);
            }
        } else if (StringUtils.isNotBlank(dictReqVO.getPath())) {
            List<DictDetailModel> byPath = staticDict.getByPath(dictReqVO.getPath());
            res.setDictionarys(byPath);
            res.setDr(true);
        }
        return res;
    }

    private boolean checkDictReqVO(DictReqVO dictReqVO) {
        if (dictReqVO == null) {
            throw new YuceExceptionBase("请求字典不能为空");
        }
        if (StringUtils.isAllBlank(dictReqVO.getModel(), dictReqVO.getPath())) {
            throw new YuceExceptionBase("请求字典的model和path至少一个有值");
        }
        return false;
    }
}

其他模型

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DictModel {
    private boolean dr;
    private String model;
    private String path;
    private List<DictDetailModel> dictionarys;

    public DictModel(String model, String path, List<DictDetailModel> dictionarys) {
        this.dr = true;
        this.model = model;
        this.path = path;
        this.dictionarys = dictionarys;
    }
}
java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DictModel {
    private boolean dr;
    private String model;
    private String path;
    private List<DictDetailModel> dictionarys;

    public DictModel(String model, String path, List<DictDetailModel> dictionarys) {
        this.dr = true;
        this.model = model;
        this.path = path;
        this.dictionarys = dictionarys;
    }
}
java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DictDetailModel {

    private String dictModel;

    private String desc;

    private List<String> dictPath;

    private Map<String,String> dictContext;
}
java 复制代码
@Data
public class DictReqVO {
    @ApiModelProperty("模型名称 优先级高于path")
    private String model;
    @ApiModelProperty(value = "业务路径", example = "/all/cube/rpa")
    private String path;
}

分析

其实就是项目一开始启动的时候把IDict下的所有的字典写入到配置中注入到spring容器中,加以标识和分类,调用业务实现类去查询非常的快

字典控制层

java 复制代码
@RestController
@RequestMapping(path = "/dict")
@Api(tags = "字典管理控制层", description = "字典管理")
@Slf4j
@Validated
public class DictController {

    @Autowired
    private IDictService dictService;

    @GetMapping("/byModelOrPath")
    @ApiOperation("查询字典")
    public Result<DictModel> listForQuery(@ModelAttribute @ApiParam DictReqVO dictReqVO) {
        return Result.success(dictService.byModelOrPath(dictReqVO));
    }
}
相关推荐
Daniel 大东32 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
不去幼儿园35 分钟前
【MARL】深入理解多智能体近端策略优化(MAPPO)算法与调参
人工智能·python·算法·机器学习·强化学习
幽兰的天空2 小时前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10225 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou5 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
安静读书5 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频
冰帝海岸6 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了7 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·7 小时前
java基础面试题笔记(基础篇)
java·笔记·python