官方文档:自定义类型转换器
是时间戳为例的代码:
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> {@Overridepublic 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> {@Overridepublic 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 {@Overrideprotected 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 forentities used to pre initialize the MappingContext. By default, theconfiguration classes package is used.*/@Overrideprotected Collection<String> getMappingBasePackages() {return super.getMappingBasePackages();}@Overrideprotected String getDatabaseName() {return "testdata";}
}