当前位置:网站首页>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 BigDecimalToDecimal28ListDatabase:

Return result:


边栏推荐
- Qt借助隐藏控件和QSS绘制重复元素
- DRM Memory Management
- 强化学习_12_Datawhale深度确定性策略梯度
- Myunity框架笔记
- 指纹浏览器在使用易路代理时常见的问题及解决办法
- 各位大佬 oracle cdc 默认配置 偶发会30秒才抓取到数据 这个怎么优化啊
- Make a boot floppy and boot with bochs emulator
- 【论文解读】滴滴智能派单-KDD2018 Large-Scale Order Dispatch in On-Demand Ride-Hailing
- OpenGL学习笔记(LearnOpenGL)-第三部分 绘制矩形
- 1413. 逐步求和得到正数的最小值
猜你喜欢
随机推荐
order by注入与limit注入,以及宽字节注入
全网可达并设备加密
Unity2d自动寻路(AI插件)
How to implement a grid construction system
OpenGL学习笔记(LearnOpenGL)-第四部分 着色器
unity守则(随时持续更新\自我总结)
ArgumentException: GetComponent requires that the requested component ‘GameObject‘ derives from Mono
DRM Memory Management
Can‘t find bundle for base name jdbc, locale zh_CN解决方法
什么是代理ip?市面上好用的代理软件有哪些
unityFps射击
XV6 swtch.S详解
socket实现进程间通信
Mysql表数据在命令行窗口下中文乱码问题解决方法
OpenGL学习笔记(LearnOpenGL)第一部分-环境配置与基础知识
vsnprint和snprintf的区别
Qt程序字体初始化引起的白屏问题
动态规划——从0-1背包问题到leetcode正则匹配
Qt使用私有接口绘制窗口阴影
1413. 逐步求和得到正数的最小值









