通过JACKSON反序列化实现转换数据库字典字段,数据库字典字段返显处理

通过JACKSON反序列化实现转换数据库字典字段

场景:

数据库有一张字典表:
type code value
1 1
1 2
一张用户表:
id gender name
1 1 张三
前端查询需要展示性别,返回值应该是男/女 而不是1,有时为了返显方便会返回code + value的形式。

实现方案:

1、MybatisPlus + Jackson,官方链接:https://mp.baomidou.com/guide/enum.html

1、 添加mybatisPlus枚举扫描:type-enums-package: com.aicity.entity.enums
2、 创建一个性别类型枚举 gender 实现 IEnum
3、 将你原来的查询DO中的性别属性改为枚举  private int gender -> private Gender gender;
4、 通过jacksong注解可以配置返回数据的类型:
	// 这种返回的是  "gender":{"code": 1, "value":"男"}
	@JsonFormat(shape = JsonFormat.Shape.OBJECT)
		public enum GenderEnum { 

//===========================分割=====================================
// 注解加在getDesc上,	这种直接返回描述  gender:"男"
@Jsonvalue
public String getDesc() {
 return this.desc;
}

2、通过jackson返序列化自定义处理,这种方式不需要每种类型都创建一个枚举,只需要定义一个通用枚举类,固定格式即可

	@JsonSerialize(using = DicSerializer.class)
    @Dic(DicTypeEnum.GENDER)
    private Integer gender;

public enum DicTypeEnum {

    /**性别*/
    GENDER(3),
	;
    private int type;

    public int getType() {
        return type;
    }

    DicTypeEnum(int type) {
        this.type = type;
    }
}

import com.aicity.common.util.bean.SpringUtils;
import com.aicity.entity.dto.DicData;
import com.aicity.service.sys.SysDicService;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
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 java.io.IOException;

/**
 * 字典类型序列化
 */
public class DicSerializer extends StdSerializer implements ContextualSerializer {

    private int type;

    public DicSerializer() {
        super(Integer.class);
    }

    public DicSerializer(int type) {
        super(Integer.class);
        this.type = type;
    }


    @Override
    public JsonSerializer createContextual(SerializerProvider prov, BeanProperty property) {
        Dic annotation = property.getAnnotation(Dic.class);
        return new DicSerializer(annotation.value().getType());
    }

    @Override
    public void serialize(Integer code, JsonGenerator gen, SerializerProvider provider) throws IOException {
        DicData dic = SpringUtils.getBean(SysDicService.class).getDicByTypeAndCode(type, code);
        gen.writeObject(dic);
    }
}


import com.aicity.entity.enums.DicTypeEnum;

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)

public @interface Dic {
    DicTypeEnum value();
}

import com.aicity.common.base.service.BaseServiceImpl;
import com.aicity.dao.sys.SysDicDao;
import com.aicity.entity.dto.DicData;
import com.aicity.entity.dao.sys.SysDicDO;
import com.aicity.service.sys.SysDicService;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysDicServiceImpl extends BaseServiceImpl implements SysDicService {

    @Autowired
    private SysDicDao sysDicDao;

	// 这里加了缓存,用的是jetcache,具体可以自己选择
    @Cached(name = "sys:dic:type-code:", expire = 1, timeUnit = TimeUnit.DAYS)
    @Override
    public DicData getDicByTypeAndCode(int type, int code) {
    	// 下面就是处理返回格式的具体实现,想封装什么对象都可以自定义
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper().eq(SysDicDO::getType, type).eq(SysDicDO::getCode, code);
        SysDicDO dicDO = sysDicDao.selectOne(queryWrapper);
        DicData dicData = new DicData();
        BeanUtils.copyProperties(dicDO, dicData);
        return dicData;
    }
}

你可能感兴趣的:(新学感悟)