当前位置:网站首页>About MongoDb query Decimal128 to BigDecimal problem
About MongoDb query Decimal128 to BigDecimal problem
2022-08-10 06:32:00 【chw-di】
Premise: use MongoTemplate to query the database, and the return result type is Map
Decimal128 cannot convert BigDecimal, unless your return type defines the type of the object as BigDecimal, then if you cannot determine what the return value is, you can only use Map to connect to the end of the return.
Solution:
You can use jackson to convert Decimal128 to BigDecimal
Second method I used:
Change the source code.
The getPotentiallyConvertedSimpleRead method in MappingMongoConverter
Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies {@link Enum} handling or returns the value as is. Can be overridden by subclasses.
Checks whether the given simple object has a custom transformation.If so, convert the given value and apply
private Object getPotentiallyConvertedSimpleRead(Object value, @Nullable Class> target) {if (target==null){return value;}//Convert Decimal128 to BigDecimalif (value.getClass() == Decimal128.class){return ((Decimal128) value).bigDecimalValue();}if (ClassUtils.isAssignableValue(target, value)) {return value;}if (conversions.hasCustomReadTarget(value.getClass(), target)) {return doConvert(value, target);}if (Enum.class.isAssignableFrom(target)) {return Enum.valueOf((Class) target, value.toString());}return doConvert(value, target);}
On the left is the unmodified code, because I used Map to connect the result, so the types are all Object, and there is a misunderstanding before checking whether there is a custom conversion, because ClassUtils was called before.isAssignableValue method
from ClassUtils.java
public static boolean isAssignableValue(Class> type, @Nullable Object value) {Assert.notNull(type, "Type must not be null");return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());}
Determine if the given type is assignable from the given value, assuming setting by reflection.
Determines whether the given type is assignable from the given value, assuming set by reflection.The primitive wrapper class is considered assignable to the corresponding primitive type.
So the code on the right is changed to bigDecimalValue() that takes Decimal128 as long as the value is Decimal128
It is very simple to convert BigDecimal to Decimal128, just write a configuration class and let MongoTemplate load it,
@ReadingConverterpublic class BigDecimalToDecimal128Converter implements Converter {@Overridepublic Decimal128 convert(BigDecimal bigDecimal) {return new Decimal128(bigDecimal);}}
private static MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);//Add conversion to BigDecimalToDecimal28List
Database:
Return result:
边栏推荐
猜你喜欢
随机推荐
UnityShader入门精要-渲染纹理 镜子 玻璃 效果
The use of pointers from the exchange of two data values (C language implementation)
Qt程序字体初始化引起的白屏问题
【8月9日活动预告】Prometheus峰会
什么是MQTT网关?与传统DTU有哪些区别?
MySQL笔记
npm搭建私服,上传下载包
unity在UI界面上展示旋转模型
elf文件与链接脚本
A*Pathfinding插件(3D)
QScroller的QScrollerProperties参数研究
ES13 - ES2022 - 第 123 届 ECMA 大会批准了 ECMAScript 2022 语言规范
UnityShader入门精要-纹理动画、顶点动画
R language cluster analysis - code analysis
椭圆曲线离散对数问题以及求解
驱动的参数传入:module_param,module_param_array,module_param_cb
Qt滚动条(QScrollBar)圆角样式问题跟踪
Share a professional TA's "Shader Reference"
QEMU guest与host通过网络通信——bridge/hostfwd/guestfwd
Log4j2基本使用