原因:雪花算法新增的信息,无法对其更新删除操作,排查了原因,发现前端传给后端的id(Long),与数据库的ID(bigint)不太一样,但很类似。
55.170 DEBUG c.y.m.E.deleteByPrimaryKey :137 http-nio-8880-exec-3 ==> Preparing: delete from ebook where id = ?
55.173 DEBUG c.y.m.E.deleteByPrimaryKey :137 http-nio-8880-exec-3 > Parameters: 98246969067180030(Long)
55.176 DEBUG c.y.m.E.deleteByPrimaryKey :137 http-nio-8880-exec-3 < Updates: 0
一番查询是雪花算法新增导致的精度丢失:
解决:
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
/**
* 通用序列化过滤器
* 与前端交互时对实体类中 Long 类型的 ID 字段序列化
* 解决雪花算法精度丢失问题
*/
@Configuration
public class FastJsonHttpMessageConverterHandler {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
List<SerializerFeature> list = new ArrayList<>();
list.add(SerializerFeature.PrettyFormat);
list.add(SerializerFeature.WriteMapNullValue);
list.add(SerializerFeature.WriteNullStringAsEmpty);
list.add(SerializerFeature.WriteNullListAsEmpty);
list.add(SerializerFeature.QuoteFieldNames);
list.add(SerializerFeature.WriteDateUseDateFormat);
list.add(SerializerFeature.DisableCircularReferenceDetect);
list.add(SerializerFeature.WriteBigDecimalAsPlain);
fastJsonConfig.setSerializerFeatures(list.toArray(new SerializerFeature[list.size()]));
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
fastJsonConfig.setSerializeFilters((ValueFilter) (object, name, value) -> {
/*if ((StringUtils.endsWith(name, "Id") || StringUtils.equals(name,"id")) && value != null
&& value.getClass() == Long.class) {*/
if (value != null && value.getClass() == Long.class ) {
Long v = (Long) value;
if (v.toString().length() > 15) {
return String.valueOf(value);
}
}
return value;
});
return new HttpMessageConverters(converter);
}
}