转换Spring Data JPA页面内容的类型 [英] Convert type of Spring Data JPA Page content

查看:226
本文介绍了转换Spring Data JPA页面内容的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Spring Data JPA,并且有一个使用 JPASpecificationExecutor< Contact> PagingAndSortingRepository< Contact,Long> C>。我向 .findAll()传递了一个规范和一个 Pageable $ c $>页面<联系>



然而,我的 Contact 实体有很多额外的字段和映射,我不需要在我的前端。所以,我有一个只包含必要字段的 ContactDto ,并且我有一个方法可以从 Contact 转换为 ContactDto

  private ContactDto convertToContactDto(Contact contact){
//做转换
}

如何使用此转换方法将页面< Contact> 转换为页面< ContactDto>



我可以获取页面< Contact> 的内容,并进行如下所示的转换。

 最后一页<联络人> contactPage = pagingAndSortingContactRepository 
.findAll(ContactSpecification.findByFirstNmLike(firstNm),pageable);

最后集合< ContactDto> contactDtos = contactPage.getContent()
.stream()
.map(this :: convertToContactDto)
.collect(Collectors.toList());

但我剩下一个集合而不是 Page ,而我不知道如何将 Collection 放入<$ c的内容中$ C>页。有没有办法做到这一点?或者有另外一种方法可以在页面<联系> 实例本身上调用转换?

解决方案

原来, Page 有它自己的 .map()方法,你可以传给它一个方法引用来做转换。

以下是我如何完成转换。

 最后一页< ContactDto> contactDtoPage = contactPage.map(this :: convertToContactDto); 

convertToContactDto 方法简单地创建并返回

  private ContactDto convertToContactDto(final Contact contact){
final ContactDto contactDto = new ContactDto();
//从联系实体获取值并将它们设置为contactDto
//例如contactDto.setContactId(contact.getContactId());
返回contactDto;
}


I am using Spring Data JPA and I have a PagingAndSortingRepository<Contact, Long> that uses a JPASpecificationExecutor<Contact>. I pass a Specification and a Pageable instance to the .findAll() method of this repository to get a Page<Contact>.

However, my Contact entity has a lot of extra fields and mappings that I don't need on my front end. So, I have a ContactDto that contains only the necessary fields, and I have a method that can convert from Contact to ContactDto.

private ContactDto convertToContactDto(Contact contact) {
    //do the conversion
}

How would I go about using this conversion method to convert the Page<Contact> to a Page<ContactDto>?

I can get the content of the Page<Contact> and do the conversion like this.

final Page<Contact> contactPage = pagingAndSortingContactRepository
        .findAll(ContactSpecification.findByFirstNmLike(firstNm), pageable);

final Collection<ContactDto> contactDtos = contactPage.getContent()
    .stream()
    .map(this::convertToContactDto)
    .collect(Collectors.toList());

But then I am left with a Collection instead of a Page, and I don't know how to get that Collection into the content of the Page. Is there a way to do this? Or is there another way to call the conversion on the Page<Contact> instance itself?

解决方案

Turns out that Page has its own .map() method, to which you can pass a method reference to do the conversion.

Here is how I ended up doing the conversion.

final Page<ContactDto> contactDtoPage = contactPage.map(this::convertToContactDto);

The convertToContactDto method simply creates and returns an instance of the class I'm trying to convert to:

private ContactDto convertToContactDto(final Contact contact) {
    final ContactDto contactDto = new ContactDto();
    //get values from contact entity and set them in contactDto
    //e.g. contactDto.setContactId(contact.getContactId());
    return contactDto;
}

这篇关于转换Spring Data JPA页面内容的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆