自己实现一个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);
}
}
}