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