MyBatis 类型处理器 TypeHandler 泛型擦除问题
问题
-
Q:使用 TypeHandler 处理
List
Map
等带泛型字段序列化 JSON 保存进去 MySQL 数据库时候发现没法反序列化还原 -
A:Java语言的泛型采用的是擦除法实现的伪泛型,泛型信息(类型变量、参数化类型)编译之后通通被除掉了。因为 List 泛型字段 编译后擦除相关类型导致出现这个问题
编写万能通用 JSON TypeHandler
万能通用转 JSON TypeHandler 代码
package io.github.hdfg159.common.handler.typehandler;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
/**
* 万能对象类型转换 json
* 注意:这个反序列化会保存类的相关信息,请不要修改反序列化类的类名和路径
*
* @author hdfg159
* @date 2021/5/26 17:00
*/
@Slf4j
@MappedJdbcTypes(value = {
JdbcType.VARCHAR}, includeNullJdbcType = true)
public class GenericJacksonJsonTypeHandler<E> extends BaseTypeHandler<E> {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
// 未知字段忽略
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 不使用科学计数
MAPPER.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
// null 值不输出(节省内存)
MAPPER.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
// 配置输出 反序列化类名的形式
MAPPER.activateDefaultTyping(
LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.PROPERTY
);
}
private final Class<E> type;
public GenericJacksonJsonTypeHandler(Class<E> type) {
Objects.requireNonNull(type);
this.type = type;
}
@Override
public