【Java教程】基于Springboot一个注解搞定数据字典的实践方案

所需工具:

Java

聪明的大脑

勤劳的双手

 

注意:本站只提供教程,不提供任何成品+工具+软件链接,仅限用于学习和研究,禁止商业用途,未经允许禁止转载/分享等

 

教程如下

问题引出:

最近开了新项目,项目中用到了数据字典,列表查询数据返回的时候需要手动将code转换为name,到前台展示。项目经理表示可以封装一个统一的功能,避免程序员各自写各自的,代码混乱,风格不统一。

要求:

基于微服务架构,数据字典通过服务获取;
简化代码,使用简单;
使用Redis;

方案

大致的方向是自定义注解,在序列化的时候进行数据处理; 考虑到微服务,需要将主要逻辑放到common中,然后对外提供接口,各业务服务实现接口以获取字典数据; 考虑Redis,序列化处理数据时,首先通过Redis获取,获取不到在通过接口获取,拿到数据后存到Redis中,然后再返回处理; 也可以多做一步,在新增、修改数据字典时,同步更新Redis内容,以保证数据有效性。

实现

定义注解

 	@Target({ElementType.FIELD})
 	@Retention(RetentionPolicy.RUNTIME)
 	@JacksonAnnotationsInside
 	@jsonSerialize(using = DictSerializer.class)
 	public @interface Dict {
 	 
 	     /** 字典类型 */
 	     String type();
 	}

指定注解添加位置
指定注解生效时间
指定序列化处理类

序列化处理类

 	public class DictSerializer extends StdSerializer<Object> implements ContextualSerializer {
 	     /** 字典注解 */
 	     private Dict dict;
 	     public DictSerializer() {
 	         super(Object.class);
 	     }
 	     public DictSerializer(Dict dict) {
 	         super(Object.class);
 	         this.dict = dict;
 	     }
 	     private String type;
 	     @Override
 	     public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
 	         if (Objects.isNull(value)) {
 	             gen.writeObject(value);
 	             return;
 	         }
 	         if (Objects.nonNull(dict)){
 	             type = dict.type();
 	         }
 	         // 通过数据字典类型和value获取name
 	 
 	         gen.writeObject(value);
 	         gen.writeFieldName(gen.getOutputContext().getCurrentName()+"Name");
 	         gen.writeObject(label);
 	     }
 	     @Override
 	     public JsonSerializer< > createContextual(SerializerProvider prov, BeanProperty beanProperty) throws JsonMappingException {
 	         if (Objects.isNull(beanProperty)){
 	             return prov.findValueSerializer(beanProperty.getType(), beanProperty);
 	         }
 	         Dict dict = beanProperty.getAnnotation(Dict.class);
 	         if (Objects.nonNull(dict)){
 	             type = dict.type();
 	             return this;
 	         }
 	         return prov.findNullValueSerializer(null);
 	     }
 	}

这里处理的逻辑是原先的字段内容不变,添加一个新的字段用来存储转化后的值;

数据字典获取

 	private static String changeLabel(String type,String code) {
 	     if(code.indexOf(",") > -1) {
 	         String[] strs = code.split(",");
 	         if (strs.length > 1) {
 	             StringBuilder sb = new StringBuilder();
 	             for (String str : strs) {
 	                 // 从缓存中获取字典。如果不行,通过SpringUtil.getBean(); 获取服务处理
 	                 sb.append(DictDataCache.getLabel(type, str)).append(separator);
 	             }
 	             return sb.substring(0, sb.length() - 1);
 	         }
 	     }
 	     // 从缓存中获取字典。如果不行,通过SpringUtil.getBean(); 获取服务处理
 	     return DictDataCache.getLabel(type, code);
 	}

考虑存在多选的情况,先判断下是否是多选的,默认逗号拼接,后期添加入参控制;

 	@Override
 	public String getDictDataOptions(String typeCode,String value) {
 	     if (redisTemplate.hasKey("dict:"+typeCode+":"+value)){
 	         return (String) redisTemplate.opsForValue().get("dict:"+typeCode+":"+value);
 	     }
 	     List<DictDataOptions> dictDataList = getDictDataHandler().getDictDataOptions(typeCode);
 	     if(CollUtil.isNotEmpty(dictDataList)) {
 	         put(typeCode, dictDataList);
 	     }
 	     if (redisTemplate.hasKey("dict:"+typeCode+":"+value)){
 	         return (String) redisTemplate.opsForValue().get("dict:"+typeCode+":"+value);
 	     }
 	     return null;
 	}

根据key判断Redis中是否存在,存在则直接获取,不存在则通过接口获取,获取到直接放到Redis中,然后再次从Redis获取。

 	protected void put(String typeCode, List<DictDataOptions> dataList) {
 	     if (CollUtil.isNotEmpty(dataList)){
 	         for (DictDataOptions dictDataOptions : dataList) {
 	             AbstractDictHandler.redisTemplate.opsForValue().set("dict:"+typeCode+":"+dictDataOptions.getDataLabel(),dictDataOptions.getDataValue());
 	         }
 	     }
 	}

循环放置数据字典值

 	@Override
 	public List<DictDataOptions> getDictDataOptions(String typeCode) {
 	     return iSysDictService.queryDictItemsByCode(typeCode).stream()
 	             .map(e -> DictDataOptions.builder().typeCode(typeCode).dataLabel(e.getValue()).dataValue(e.getText()).build())
 	             .collect(Collectors.toList());
 	}

根据数据字典类型,通过接口获取数据;注意该实现类需要每个微服务实现一个;然后为了避免基础数据服务挂掉,调用报错,common中提供一个默认实现。

4.使用

 	@Dict(type = "inspectType")
 	private String checkType;

在返回前端的实体中,对应字段添加注解,并指定数据字典type值

 	         {
 	         "id": "1522492702905954306",
 	         "professionName": "专业名称888",
 	         "checkCode": "检测项编码8",
 	         "checkProject": "rrrr检测项目88",
 	         "checkDevice": "52",
 	         "checkStandard": "检测项编码88",
 	         "referenceStandard": "wq参考标准8",
 	         "checkType": "1",
 	         "checkTypeName": "尺寸",
 	         "remarks": "ef备注备注8"
 	         },

前端获取的json会多一个字段:checkTypeName,内容为checkType 的中文值。

标签

发表评论