【无标题】

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;
}
相关推荐
CodeCraft Studio1 小时前
PPT处理控件Aspose.Slides教程:在 C# 中将 PPTX 转换为 Markdown
开发语言·c#·powerpoint·markdown·ppt·aspose·ai大模型
萧鼎2 小时前
深入理解 Python Scapy 库:网络安全与协议分析的瑞士军刀
开发语言·python·web安全
Moss Huang3 小时前
docker-runc not installed on system
java·docker·容器
阿拉丁的梦4 小时前
教程1:用vscode->ptvsd-创建和调试一个UI(python)-转载官方翻译(有修正)
开发语言·python
木宇(记得热爱生活)4 小时前
一键搭建开发环境:制作bash shell脚本
开发语言·bash
麦兜*4 小时前
Spring Boot 集成 Docker 构建与发版完整指南
java·spring boot·后端·spring·docker·系统架构·springcloud
Cisyam^4 小时前
Go环境搭建实战:告别Java环境配置的复杂
java·开发语言·golang
CHENFU_JAVA5 小时前
使用EasyExcel实现Excel单元格保护:自由锁定表头和数据行
java·excel
IAR Systems6 小时前
在IAR Embedded Workbench for Arm中实现Infineon TRAVEO™ T2G安全调试
开发语言·arm开发·安全·嵌入式软件开发·iar