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;
}