• <bdo id='5HIjM'></bdo><ul id='5HIjM'></ul>
    1. <tfoot id='5HIjM'></tfoot>

      <legend id='5HIjM'><style id='5HIjM'><dir id='5HIjM'><q id='5HIjM'></q></dir></style></legend>
    2. <small id='5HIjM'></small><noframes id='5HIjM'>

    3. <i id='5HIjM'><tr id='5HIjM'><dt id='5HIjM'><q id='5HIjM'><span id='5HIjM'><b id='5HIjM'><form id='5HIjM'><ins id='5HIjM'></ins><ul id='5HIjM'></ul><sub id='5HIjM'></sub></form><legend id='5HIjM'></legend><bdo id='5HIjM'><pre id='5HIjM'><center id='5HIjM'></center></pre></bdo></b><th id='5HIjM'></th></span></q></dt></tr></i><div id='5HIjM'><tfoot id='5HIjM'></tfoot><dl id='5HIjM'><fieldset id='5HIjM'></fieldset></dl></div>

      Java 8 日期/时间 (JSR-310) 类型与 Spring Data MongoDB 的映射

      时间:2024-08-24
        <tbody id='iL5pY'></tbody>

          <legend id='iL5pY'><style id='iL5pY'><dir id='iL5pY'><q id='iL5pY'></q></dir></style></legend>

          <small id='iL5pY'></small><noframes id='iL5pY'>

            <bdo id='iL5pY'></bdo><ul id='iL5pY'></ul>

                <tfoot id='iL5pY'></tfoot>
                <i id='iL5pY'><tr id='iL5pY'><dt id='iL5pY'><q id='iL5pY'><span id='iL5pY'><b id='iL5pY'><form id='iL5pY'><ins id='iL5pY'></ins><ul id='iL5pY'></ul><sub id='iL5pY'></sub></form><legend id='iL5pY'></legend><bdo id='iL5pY'><pre id='iL5pY'><center id='iL5pY'></center></pre></bdo></b><th id='iL5pY'></th></span></q></dt></tr></i><div id='iL5pY'><tfoot id='iL5pY'></tfoot><dl id='iL5pY'><fieldset id='iL5pY'></fieldset></dl></div>
              • 本文介绍了Java 8 日期/时间 (JSR-310) 类型与 Spring Data MongoDB 的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个带有 Java 8 日期/时间字段的简单文档

                I have simple document with Java 8 date/time fields

                @Document
                public class Token {
                    private Instant createdAt;
                    ...
                }
                

                我想坚持使用 Spring Data MongoDB 1.5 版.但是 java.time.Instant 类型的字段无法正确反序列化,因为 MappingMongoConverter 缺少 java.time 类的转换器.

                that I want to persist with Spring Data MongoDB version 1.5. But fields of type java.time.Instant could not be de-serialized correctly because MappingMongoConverter lacks converters for java.time classes.

                在 Spring 4 中,我发现 org.springframework.format.datetime.standard.DateTimeConverters 具有不同的 Converter,包括 InstantToLongConverterLongToInstantConverter 声明为私有静态类.

                In Spring 4 I found org.springframework.format.datetime.standard.DateTimeConverters with different Converters including InstantToLongConverter and LongToInstantConverter declared as private static classes.

                如何配置 MongoTemplate 以使用它们将 Instant 字段映射到 long?

                How can I configure MongoTemplate to use them to map Instant fields to longs?

                推荐答案

                我不知道这是否是最好的方法,但我在 Spring Data MongoDB 中添加了 Java 8 Date/Time (JSR-310) 类型支持 1.5.0.RELEASE 像这样:

                I don't know if this is the best way but I added Java 8 Date/Time (JSR-310) types support to Spring Data MongoDB 1.5.0.RELEASE like this:

                1. 第一步.添加简单的 Spring Converters

                public class InstantToLongConverter implements Converter<Instant, Long> {
                    @Override
                    public Long convert(Instant instant) {
                        return instant.toEpochMilli();
                    }
                }
                
                public class LongToInstantConverter implements Converter<Long, Instant> {
                    @Override
                    public Instant convert(Long source) {
                        return Instant.ofEpochMilli(source);
                    }
                }
                
                public class LocalDateToStringConverter implements Converter<LocalDate, String> {
                    @Override
                    public String convert(LocalDate localDate) {
                        return localDate.toString();
                    }
                }
                
                public class StringToLocalDateConverter implements Converter<String, LocalDate> {
                    @Override
                    public LocalDate convert(String source) {
                        return LocalDate.parse(source);
                    }
                }
                

              • 第二步.在 AbstractMongoConfiguration 实现中使用 MappingMongoConverter 注册这些自定义 Converter,如下所示:

              • Second step. Register these custom Converters with MappingMongoConverter in your AbstractMongoConfiguration implementation like this:

                @Configuration
                @EnableMongoRepositories(basePackages = {"my.app.repository"})
                public class MongoConfiguration extends AbstractMongoConfiguration {
                
                    ...
                
                    @Override
                    public CustomConversions customConversions() {
                        return new CustomConversions(Arrays.asList(
                                new InstantToLongConverter(), new LongToInstantConverter(),
                                new LocalDateToStringConverter(), new StringToLocalDateConverter()));
                    }
                }
                

              • 现在您的文档的 Instant 字段将作为长值和 LocalDates 作为字符串持久化.

                Now your document's Instant fields will be persisted as long values and LocalDates as Strings.

                这篇关于Java 8 日期/时间 (JSR-310) 类型与 Spring Data MongoDB 的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在使用带有 Spring 数据的 MongoRepository 的查询注释时显示查询 下一篇:如何为 @MappedSuperclass 实现 Spring Data 存储库

                相关文章

                <tfoot id='Hlw0M'></tfoot>
                  <bdo id='Hlw0M'></bdo><ul id='Hlw0M'></ul>
              • <small id='Hlw0M'></small><noframes id='Hlw0M'>

                <legend id='Hlw0M'><style id='Hlw0M'><dir id='Hlw0M'><q id='Hlw0M'></q></dir></style></legend>

                  <i id='Hlw0M'><tr id='Hlw0M'><dt id='Hlw0M'><q id='Hlw0M'><span id='Hlw0M'><b id='Hlw0M'><form id='Hlw0M'><ins id='Hlw0M'></ins><ul id='Hlw0M'></ul><sub id='Hlw0M'></sub></form><legend id='Hlw0M'></legend><bdo id='Hlw0M'><pre id='Hlw0M'><center id='Hlw0M'></center></pre></bdo></b><th id='Hlw0M'></th></span></q></dt></tr></i><div id='Hlw0M'><tfoot id='Hlw0M'></tfoot><dl id='Hlw0M'><fieldset id='Hlw0M'></fieldset></dl></div>