springboot与mongodb之BigDecimal类型转化(四)

一、说明

mongodb支持Decimal128不支持BigDecimal类型字段,需要自定义转化。

二、创建BigDecimal转化Decimal128

import org.bson.types.Decimal128;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;

import java.math.BigDecimal;

/**
 * 转化处理类
 **/
@ReadingConverter
@WritingConverter
public class BigDecimalToDecimal128Converter implements Converter<BigDecimal, Decimal128> {
    @Override
    public Decimal128 convert(BigDecimal source) {
        return new Decimal128(source);
    }
}

三、创建Decimal128转化BigDecimal

import org.bson.types.Decimal128;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;

import java.math.BigDecimal;

/**
 * 处理类
 *
*/
@ReadingConverter
@WritingConverter
public class Decimal128ToBigDecimalConverter implements Converter<Decimal128, BigDecimal> {
    @Override
    public BigDecimal convert(Decimal128 source) {
        return source.bigDecimalValue();
    }
}

四、添加配置类

import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.ArrayList;
import java.util.List;

/**
 * mongodb配置类
 *
*/
@Configuration
@Slf4j
public class MongodbConfig {
    
    /**
     * 自定义转换
     *
     * @param mongoMappingContext mongoMappingContext
     * @param factory factory
     * @return 转换
     */
    @Bean
    public MappingMongoConverter mappingMongoConverter(MongoMappingContext mongoMappingContext,
        MongoDatabaseFactory factory) {
        mongoMappingContext.setAutoIndexCreation(true);
        mongoMappingContext.afterPropertiesSet();
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        // 此处是去除插入数据库的 _class 字段
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        List<Object> list = new ArrayList<>();
        list.add(new BigDecimalToDecimal128Converter());
        list.add(new Decimal128ToBigDecimalConverter());
        converter.setCustomConversions(new MongoCustomConversions(list));
        return converter;
    }

    /**
     * 上下文
     *
     * @return 上下文
     */
    @Bean
    public MongoMappingContext mongoMappingContext() {
        // 必须重新注入
        return new MongoMappingContext();
    }
}

然后就可以正常使用了。。。

posted @ 2023-05-04 17:00  薛大坑  阅读(445)  评论(0编辑  收藏  举报