MP JacksonTypeHandler 需要Long类型List却处理成了Integer类型List怎么办

自己实现一个TypeHandler用于专门处理List<Long>类型的数据

import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
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.util.List;

/**
 * Jackson 实现 List< Long> 字段类型处理器
 *
 * @author wuhan
 * @since 2024-3-28
 */
@Slf4j
@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
@AllArgsConstructor
public class ListLongJacksonHandler extends AbstractJsonTypeHandler<Object> {
    private final ObjectMapper objectMapper;
    @Override
    protected Object parse(String json) {
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Long.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

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

还有:也可以改用Spring Data JPA

引用

https://github.com/baomidou/mybatis-plus/issues/2539

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议
上一篇