基于Springboot一個注解搞定數(shù)據(jù)字典的實踐方案
問題引出:
最近開了新項目,項目中用到了數(shù)據(jù)字典,列表查詢數(shù)據(jù)返回的時候需要手動將code轉換為name,到前臺展示。項目經(jīng)理表示可以封裝一個統(tǒng)一的功能,避免程序員各自寫各自的,代碼混亂,風格不統(tǒng)一。
要求:
- 基于微服務架構,數(shù)據(jù)字典通過服務獲??;
- 簡化代碼,使用簡單;
- 使用Redis;
方案
大致的方向是自定義注解,在序列化的時候進行數(shù)據(jù)處理; 考慮到微服務,需要將主要邏輯放到common中,然后對外提供接口,各業(yè)務服務實現(xiàn)接口以獲取字典數(shù)據(jù); 考慮Redis,序列化處理數(shù)據(jù)時,首先通過Redis獲取,獲取不到在通過接口獲取,拿到數(shù)據(jù)后存到Redis中,然后再返回處理; 也可以多做一步,在新增、修改數(shù)據(jù)字典時,同步更新Redis內(nèi)容,以保證數(shù)據(jù)有效性。
實現(xiàn)
- 定義注解
@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(); } // 通過數(shù)據(jù)字典類型和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); } }
這里處理的邏輯是原先的字段內(nèi)容不變,添加一個新的字段用來存儲轉化后的值;
- 數(shù)據(jù)字典獲取
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); }
考慮存在多選的情況,先判斷下是否是多選的,默認逗號拼接,后期添加入?yún)⒖刂疲?/p>
@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; }
根據(jù)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()); } } }
循環(huán)放置數(shù)據(jù)字典值
@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()); }
根據(jù)數(shù)據(jù)字典類型,通過接口獲取數(shù)據(jù);注意該實現(xiàn)類需要每個微服務實現(xiàn)一個;然后為了避免基礎數(shù)據(jù)服務掛掉,調用報錯,common中提供一個默認實現(xiàn)。
4.使用
@Dict(type = "inspectType") private String checkType;
在返回前端的實體中,對應字段添加注解,并指定數(shù)據(jù)字典type值
{ "id": "1522492702905954306", "professionName": "專業(yè)名稱888", "checkCode": "檢測項編碼8", "checkProject": "rrrr檢測項目88", "checkDevice": "52", "checkStandard": "檢測項編碼88", "referenceStandard": "wq參考標準8", "checkType": "1", "checkTypeName": "尺寸", "remarks": "ef備注備注8" },
前端獲取的json會多一個字段:checkTypeName,內(nèi)容為checkType 的中文值。
到此這篇關于基于Springboot一個注解搞定數(shù)據(jù)字典問題的文章就介紹到這了,更多相關Springboot數(shù)據(jù)字典內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis-plus QueryWrapper 添加limit方式
這篇文章主要介紹了mybatis-plus QueryWrapper 添加limit方式,具有很好的參考價值,希望對大家有所2022-01-01java IO流 之 輸入流 InputString()的使用
這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯,需要的朋友可以參考下2016-12-12Centos 7 安裝 OpenJDK 11 兩種方式及問題小結
這篇文章主要介紹了Centos 7 安裝 OpenJDK 11 兩種方式,第一種方式使用yum安裝,第二種方式使用tar解壓安裝,每種方法給大家介紹的非常詳細,需要的朋友可以參考下2021-09-09淺析SpringMVC中的適配器HandlerAdapter
這篇文章主要介紹了SpringMVC中的適配器HandlerAdapter的相關資料,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01淺談Java內(nèi)存區(qū)域與對象創(chuàng)建過程
下面小編就為大家?guī)硪黄獪\談Java內(nèi)存區(qū)域與對象創(chuàng)建過程。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07