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

