【无标题】

java 复制代码
实体类中字典映射
1.定义注解
package com.perye.dokit.entity.dictSet;

import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = DictBizOneSerialize.class)
public @interface DictBizOne {

    String type();
}
2.获取字典
package com.perye.dokit.entity.dictSet;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.perye.dokit.entity.DictDetail;
import com.perye.dokit.query.DictDetailQueryCriteria;
import com.perye.dokit.service.DictDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Objects;

/**
 * @author hua
 * @description
 */
@Slf4j
public class DictBizOneSerialize extends StdSerializer<Object> implements ContextualSerializer {
    @Autowired
   private DictDetailService dictDetailService;
    //字典注解
    private DictBizOne dict;
    public DictBizOneSerialize() {
        super(Object.class);
    }
    public DictBizOneSerialize(DictBizOne 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()+"Label");
        //字典赋值(不同项目的字典值获取会不一样,个人根据实际情况实现)
        DictDetailQueryCriteria dq =new DictDetailQueryCriteria();
        dq.setDictName(type);
        dq.setValue(String.valueOf(value));
        DictDetail dd =  dictDetailService.queryObject(dq);
        String label = dd.getLabel();
        gen.writeObject(label);
    }
    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty beanProperty) throws JsonMappingException {
        if (Objects.isNull(beanProperty)){
            return prov.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        DictBizOne dict = beanProperty.getAnnotation(DictBizOne.class);
        if (Objects.nonNull(dict)){
            type = dict.type();
            return this;
        }
        return prov.findNullValueSerializer(null);
    }
}
//从数据库中获取字典值
   @Cacheable
    @Override
    public DictDetail queryObject(DictDetailQueryCriteria criteria) {
        List<DictDetail> list = dictDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        if(list.size()>0){
            return list.get(0);
        }
       return  new DictDetail();
    }
根据name和value
@Data
public class DictDetailQueryCriteria {

    @Query(type = Query.Type.INNER_LIKE)
    private String label;
    @Query(type = Query.Type.EQUAL)
    private String value;

    @Query(propName = "name",joinName = "dict")
    private String dictName;
}
相关推荐
吾日三省吾码37 分钟前
JVM 性能调优
java
stm 学习ing42 分钟前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc2 小时前
《Python基础》之字符串格式化输出
开发语言·python
弗拉唐2 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi772 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
少说多做3432 小时前
Android 不同情况下使用 runOnUiThread
android·java