最近遇到一个问题 就是 BSON
存储到 Mongodb
的时候 BigDacemel
查阅文档 + 看源码 找了一个简单的方案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package pw.yumc.cloud.core.config;
import java.math.BigDecimal; import java.util.ArrayList; import java.util.List;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
@Configuration public class MongoDbCustomConversions { @Bean public MongoCustomConversions mongoCustomConversions() { List<Converter> converters = new ArrayList<>(); converters.add(new Converter<BigDecimal, Double>() { @Override public Double convert(BigDecimal bigDecimal) {return bigDecimal.doubleValue();} }); converters.add(new Converter<Double, BigDecimal>() { @Override public BigDecimal convert(Double val) { return new BigDecimal(val); } }); return new MongoCustomConversions(converters); } }
|