当前位置:网站首页>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:
边栏推荐
猜你喜欢
随机推荐
复现dns外带数据结合sqlmap
Kernel performance analysis summary
各位大佬,oracle11g,cdc2.2,flink1.13.6,单表增量同步。在没新增数据的情
动态规划——从0-1背包问题到leetcode正则匹配
【强化学习】《Easy RL》- Q-learning - CliffWalking(悬崖行走)代码解读
Kernel Image File Format
Log4j2基本使用
腾讯云宋翔:Kubernetes集群利用率提升实践
Unity导航与寻路系统的基本使用
Qt绘制椭圆曲线的角度问题(离心角和旋转角)
Using parseInt() in TypeScript
A*Pathfinding插件(3D)
全网可达并设备加密
Unity screen coordinates to world coordinates, mouse click to get 3D position
全网可达,实现备份
unity守则(随时持续更新\自我总结)
UE 游戏模式
Talking about 3 common shadow rendering techniques in games (1): plane shadow
交换机的功能和ipv4
Can‘t find bundle for base name jdbc, locale zh_CN解决方法