基于Springboot一個(gè)注解搞定數(shù)據(jù)字典的實(shí)踐方案
問題引出:
最近開了新項(xiàng)目,項(xiàng)目中用到了數(shù)據(jù)字典,列表查詢數(shù)據(jù)返回的時(shí)候需要手動(dòng)將code轉(zhuǎn)換為name,到前臺(tái)展示。項(xiàng)目經(jīng)理表示可以封裝一個(gè)統(tǒng)一的功能,避免程序員各自寫各自的,代碼混亂,風(fēng)格不統(tǒng)一。
要求:
- 基于微服務(wù)架構(gòu),數(shù)據(jù)字典通過服務(wù)獲?。?/li>
- 簡化代碼,使用簡單;
- 使用Redis;
方案
大致的方向是自定義注解,在序列化的時(shí)候進(jìn)行數(shù)據(jù)處理; 考慮到微服務(wù),需要將主要邏輯放到common中,然后對(duì)外提供接口,各業(yè)務(wù)服務(wù)實(shí)現(xiàn)接口以獲取字典數(shù)據(jù); 考慮Redis,序列化處理數(shù)據(jù)時(shí),首先通過Redis獲取,獲取不到在通過接口獲取,拿到數(shù)據(jù)后存到Redis中,然后再返回處理; 也可以多做一步,在新增、修改數(shù)據(jù)字典時(shí),同步更新Redis內(nèi)容,以保證數(shù)據(jù)有效性。
實(shí)現(xiàn)
- 定義注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = DictSerializer.class)
public @interface Dict {
/** 字典類型 */
String type();
}- 指定注解添加位置
- 指定注解生效時(shí)間
- 指定序列化處理類
- 序列化處理類
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)容不變,添加一個(gè)新的字段用來存儲(chǔ)轉(zhuǎn)化后的值;
- 數(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(); 獲取服務(wù)處理
sb.append(DictDataCache.getLabel(type, str)).append(separator);
}
return sb.substring(0, sb.length() - 1);
}
}
// 從緩存中獲取字典。如果不行,通過SpringUtil.getBean(); 獲取服務(wù)處理
return DictDataCache.getLabel(type, code);
}考慮存在多選的情況,先判斷下是否是多選的,默認(rèn)逗號(hào)拼接,后期添加入?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ù);注意該實(shí)現(xiàn)類需要每個(gè)微服務(wù)實(shí)現(xiàn)一個(gè);然后為了避免基礎(chǔ)數(shù)據(jù)服務(wù)掛掉,調(diào)用報(bào)錯(cuò),common中提供一個(gè)默認(rèn)實(shí)現(xiàn)。
4.使用
@Dict(type = "inspectType") private String checkType;
在返回前端的實(shí)體中,對(duì)應(yīng)字段添加注解,并指定數(shù)據(jù)字典type值
{
"id": "1522492702905954306",
"professionName": "專業(yè)名稱888",
"checkCode": "檢測(cè)項(xiàng)編碼8",
"checkProject": "rrrr檢測(cè)項(xiàng)目88",
"checkDevice": "52",
"checkStandard": "檢測(cè)項(xiàng)編碼88",
"referenceStandard": "wq參考標(biāo)準(zhǔn)8",
"checkType": "1",
"checkTypeName": "尺寸",
"remarks": "ef備注備注8"
},前端獲取的json會(huì)多一個(gè)字段:checkTypeName,內(nèi)容為checkType 的中文值。
到此這篇關(guān)于基于Springboot一個(gè)注解搞定數(shù)據(jù)字典問題的文章就介紹到這了,更多相關(guān)Springboot數(shù)據(jù)字典內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請(qǐng)求中某個(gè)接口需要升級(jí),正常做法是升級(jí)我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03
Java Dubbo協(xié)議下的服務(wù)端線程使用詳解
Dubbo是阿里開源項(xiàng)目,國內(nèi)很多互聯(lián)網(wǎng)公司都在用,已經(jīng)經(jīng)過很多線上考驗(yàn)。Dubbo內(nèi)部使用了Netty、Zookeeper,保證了高性能高可用性,使用Dubbo可以將核心業(yè)務(wù)抽取出來,作為獨(dú)立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心2023-03-03
關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問題
這篇文章主要介紹了關(guān)于JSONObject.toJSONString出現(xiàn)地址引用問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實(shí)現(xiàn)CSV格式轉(zhuǎn)對(duì)象
csv全稱“Comma-Separated Values”,是一種逗號(hào)分隔值格式的文件,常用來存儲(chǔ)數(shù)據(jù)的純文本格式文件。本文將用Java語言實(shí)現(xiàn)CSV轉(zhuǎn)對(duì)象,需要的可以參考一下2022-06-06
springboot中通過jwt令牌校驗(yàn)及前端token請(qǐng)求頭進(jìn)行登錄攔截實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于springboot中如何通過jwt令牌校驗(yàn)及前端token請(qǐng)求頭進(jìn)行登錄攔截的相關(guān)資料,需要的朋友可以參考下2024-08-08
Java實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證源碼示例分享
本篇文章主要介紹了Java實(shí)現(xiàn)身份證號(hào)碼驗(yàn)證源碼示例分享,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

