地图从Spring Data的页面是如何工作的?

huangapple 未分类评论46阅读模式
英文:

How does map from Page from Spring data works?

问题

正如标题所述,org.springframework.data.domain.Page 中的 map 方法是如何工作的?

根据文档,它建议您将转换器作为 map 参数,然后您会获得一个包含转换内容的新映射。根据这种解释,我编写了以下代码。

Page<T> results = getPagedResults();
return results.map(x -> {
  x.setElement("some constant");
  return x;
});

然而,上述代码并没有按预期工作。我得到了一个页面,其中包含所有其他值,如计数等,但内容是一个空列表!我实际上不得不以这种方式编写代码。

Page<T> results = getPagedResults();
return new Page<T>() {
  @Override
  public int getTotalPages() {
    return results.getTotalPages();
  }
  
  ...

  @Override
  public List<T> getContent() {
    List<T> contents = result.getContent();
    for (T content : contents) {
      content.setElement("some constant");
    }
    return contents;
  }
 
  ...
};

第二种选择冗长,似乎做了冗余的工作。我本来期望第一段代码能够实现相同的功能,但实际上并没有。

我是否误解了文档?如果不能像我使用的方式那样使用 Page 的 map 函数,那么在什么情况下会使用它呢?

英文:

As the title says how does the map from org.springframework.data.domain.Page work?

As per the documentation, it suggests that you put a converter as a map parameter, and you get a new map with the converted contents. As per this interpretation, I wrote code to something as follows.

Page&lt;T&gt; results = getPagedResults();
return results.map(x -&gt; {
  x.setElement(&quot;some constant&quot;);
  return x;
});

However the above did not work as expected. I got the Page with all the other values intact like count and so on, but the content was an empty list! I actually had to write code this way.

Page&lt;T&gt; results = getPagedResults();
return new Page&lt;T&gt;() {
  @Override
  public int getTotalPages() {
    return results.getTotalPages();
  }
  
  ...

  @Override
  public List&lt;T&gt; getContent() {
    List&lt;T&gt; contents = result.getContent();
    for (T content : contents) {
      content.setElement(&quot;some constant&quot;);
    }
    return contents;
  }
 
  ...
};

The second choice is verbose and seems to do redundant work. I would have expected the first piece of code to do the same, but it did not.

Am I reading the documentation wrong? And where would you use the map function of Page if it is not supposed to be used, the way I was using it?

答案1

得分: 0

如何尝试使用如下真实的转换器

```java
        Page<T> results = getPagedResults();
        Page<T> convertedResults = results.map(new Converter<T, T>() {
            @Override
            public T convert(T page) {
                Page page2 = new Page();
                page2.setElement('某个常量');
                //设置其他字段

                return page2;
            }
        });

<details>
<summary>英文:</summary>

How about putting a real converter like below:

    Page&lt;T&gt; results = getPagedResults();
    Page&lt;T&gt; convertedResults = results.map(new Converter&lt;T, T&gt;() {
        @Override
        public T convert(T page) {
            Page page2 = new Page();
            page2.setElement(&#39;some constant&#39;);
            //set other fields

            return page2;
        }
    });

</details>



# 答案2
**得分**: 0

你可以像我们平常使用的方式一样使用 map:

```java
results.map(mapper::convert)

其中 convert 是 mapper 的方法,用于将数据库实体转换为 DTO 或其他任何对象。

英文:

You can use map the same way as we do:

results.map(mapper::convert)

where the convert is the method of mapper to convert database entity to DTO or any another object.

huangapple
  • 本文由 发表于 2020年3月16日 08:47:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/60699127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定