【无标题】

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;
}
相关推荐
老猿讲编程14 分钟前
一个例子来说明Ada语言的实时性支持
开发语言·ada
Chrikk1 小时前
Go-性能调优实战案例
开发语言·后端·golang
幼儿园老大*1 小时前
Go的环境搭建以及GoLand安装教程
开发语言·经验分享·后端·golang·go
canyuemanyue1 小时前
go语言连续监控事件并回调处理
开发语言·后端·golang
杜杜的man1 小时前
【go从零单排】go语言中的指针
开发语言·后端·golang
测开小菜鸟1 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity2 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天2 小时前
java的threadlocal为何内存泄漏
java
caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
萧鼎3 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步