Spring 使用 MongoDB 时的数据类型转换器

2023-12-14 17:47:03

官方文档:自定义类型转换器

是时间戳为例的代码:

TimestampReadConverter.java

import org.bson.BsonTimestamp;
import org.springframework.core.convert.converter.Converter;

import java.time.Instant;
import java.util.Date;

/*
* Java类型与 MongoDB 数据类型的对应关系
*
* 如果 MongoDB 集合中字段类型是 timestamp ,则对应Java端的 BsonTimestamp,精确到秒,说主要是 MongoDB 内部使用;
* 应用如果要记录时间,可以使用 Date 类型(精确到毫秒),对应Java端的 Instant / LocalDateTime,无需自定义转换器
* */

public class TimestampReadConverter implements Converter<BsonTimestamp, Date> {
    @Override
    public Date convert(BsonTimestamp source) {
//        System.out.println("BsonTimestamp 转 Date");
        //Bson规范 :https://bsonspec.org/spec.html
        // UTC datetime - The int64 is UTC milliseconds since the Unix epoch.
        // Timestamp - Special internal type used by MongoDB replication and sharding.
        // First 4 bytes are an increment, second 4 are a timestamp.
        Instant instant = Instant.ofEpochSecond(source.getTime());
        return Date.from(instant);
    }
}

TimestampWriteConverter.java

import org.bson.BsonTimestamp;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;

import java.util.Date;

@WritingConverter
public class TimestampWriteConverter implements Converter<Date, BsonTimestamp> {
    @Override
    public BsonTimestamp convert(Date source) {
//        System.out.println("Date 转 BsonTimestamp");
        return new BsonTimestamp((int) source.toInstant().getEpochSecond(), 0);
    }
}

MongoDBConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;

import java.util.Collection;

@Configuration
public class MongoDBConfig extends AbstractMongoClientConfiguration {

    @Override
    protected void configureConverters(MongoCustomConversions.MongoConverterConfigurationAdapter adapter) {
        //用 Lambda 表达式,编译不过
//        adapter.registerConverter((BsonTimestamp source) -> Date.from(Instant.ofEpochSecond(source.getTime())));
//        adapter.registerConverter((Date source) -> new BsonTimestamp((int) source.toInstant().getEpochSecond(), 0));

        adapter.registerConverter(new TimestampReadConverter());
        adapter.registerConverter(new TimestampWriteConverter());
    }

    /* The mapping base package defines the root path used to scan for
    entities used to pre initialize the MappingContext. By default, the
    configuration classes package is used.*/
    @Override
    protected Collection<String> getMappingBasePackages() {
        return super.getMappingBasePackages();
    }

    @Override
    protected String getDatabaseName() {
        return "testdata";
    }
}

文章来源:https://blog.csdn.net/jjf19891208/article/details/134999659
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。