Jackson使用笔记

使用Mybatis-plus的JacksonTypeHandler无法反序列化List
模拟JacksonTypeHandler类写了自定义ListTypeHandler来解析,目前需要给每个List都写一个继承自ListTypeHandler的ListBeanTypeHandler
ListTypeHandler类如下:

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * Created by angryfun on 2020/7/7.
 */
@Slf4j
@MappedTypes({List.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public abstract class ListTypeHandler<T> extends AbstractJsonTypeHandler<Object> {
    protected static ObjectMapper objectMapper = new ObjectMapper();
    private Class<?> type;

    public ListTypeHandler(){}

    public ListTypeHandler(Class<?> type) {
        if(log.isTraceEnabled()) {
            log.trace("JacksonTypeHandler(" + type + ")");
        }

        Assert.notNull(type, "Type argument cannot be null", new Object[0]);
        this.type = type;
    }

    protected Object parse(String json) {
        try {
            Type type = getSuperGenricTypes(this.getClass());
            JavaType javaType = objectMapper.getTypeFactory().constructType(type);
            return objectMapper.readValue(json, javaType);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    protected String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static void setObjectMapper(ObjectMapper objectMapper) {
        Assert.notNull(objectMapper, "ObjectMapper should not be null", new Object[0]);
        objectMapper = objectMapper;
    }
    
    public static Type getSuperGenricTypes(final Class<?> clz) {
        Class<?> superClass = clz;
        Type type = superClass.getGenericSuperclass();
        while (superClass != Object.class && !(type instanceof ParameterizedType)) {
            superClass = superClass.getSuperclass();
            type = superClass.getGenericSuperclass();
        }
        if (superClass == Object.class) {
            throw new IllegalArgumentException("父类不含泛型类型:" + clz);
        }
        return ((ParameterizedType) type).getActualTypeArguments()[0];
    }
}

ListUserTypeHandler如下:
写法一,简单只需要继承自ListTypeHandler并且指定泛型类型即可:

public class ListUserTypeHandler extends ListTypeHandler<List<User>>{}

写法二: 覆盖parse方法,效率上会有所提升

public class ListUserTypeHandler extends ListTypeHandler<List<User>>{
    private TypeReference typeReference = new TypeReference<List<User>>() {};

    /**
     * 覆盖方法是为了加快速度,父方法每次都需要反射实例得到JavaType存在性能损耗
     * @param json
     * @return
     */
    @Override
    protected Object parse(String json) {
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }
}

使用方法如下,美中不足是需要给每一种List定义各自的TypeHandler

@TableField(typeHandler = ListUserTypeHandler.class)

你可能感兴趣的:(mysql,mybatis,java)