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


边栏推荐
猜你喜欢

裸辞—躺平—刷题—大厂(Android面试的几大技巧)

Talking about 3 common shadow rendering techniques in games (2): shadow cone

Talking about 3 Common Shadow Rendering Techniques in Games (3): Shadow Mapping

Talking about 3 common shadow rendering techniques in games (1): plane shadow

vscode + ccls环境配置

【8月9日活动预告】Prometheus峰会

腾讯云宋翔:Kubernetes集群利用率提升实践

Myunity框架笔记2

直接跳转与间接跳转

指纹浏览器在使用易路代理时常见的问题及解决办法
随机推荐
数据库学习之表的约束
ebp/栈帧/call stack
Myunity框架笔记
order by注入与limit注入,以及宽字节注入
Teach you to change the kernel source code--sysfs virtual file system 1
Parallax Mapping: More Realistic Texture Detail Representation (Part 1): Why Use Parallax Mapping
unityFps射击
进制的前缀表示和后缀表示
驱动的参数传入:module_param,module_param_array,module_param_cb
强化学习_10_Datawhale稀疏奖励
[网络安全]实操AWVS靶场复现CSRF漏洞
不同场景如何使用动态代理?
强化学习_06_DataWhale深度Q网络
C语言文件操作
Unity资源热更新--资源管理、Addressable
强化学习_11_Datawhale模仿学习
交换机的功能和ipv4
How to implement a grid construction system
A*Pathfinding插件(3D)
第12章 数据库其它调优策略【2.索引及调优篇】【MySQL高级】