- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 spring 数据 JPA。
我的 Controller 如下所示
@RequestMapping(value = "/pages/{pageNumber}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Page<User>> paginatedUser(@PathVariable final Integer pageNumber)
{
final PageRequest request = new PageRequest(pageNumber - 1, DEFAULt_PAGE_SIZE, Sort.Direction.DESC, "startTime");
return new ResponseEntity<>(userRepository.findAll(request), HttpStatus.OK);
}
现在我决定发送一个 PageDTO 对象而不是 Page 对象来限制发送。有什么方法可以使用 java 8 将 Page 转换为 PageDTO。
我看到 Page 派生自 Iterable
所以我想我可以做类似下面的事情,但不确定如何将它与 PageDTO 和 UserDTO 放在一起。
StreamSupport.stream(userRepository.findAll(request).spliterator(),false)
是否有任何有效的 Java 8 方法来执行此操作。
我想到了这个解决方案
@RequestMapping(value = "/pages/{pageNumber}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<PageDTO> paginatedUser(@PathVariable final Integer pageNumber)
{
final PageRequest request = new PageRequest(pageNumber - 1, DEFAULt_PAGE_SIZE, Sort.Direction.DESC, "startTime");
final Page<User> page = userRepository.findAll(request);
return new ResponseEntity<>(new PageDTO(page, StreamSupport.stream(page.getContent().spliterator(), true).map(UserDTO::new)
.collect(Collectors.toList())), HttpStatus.OK);
}
public class PageDTO {
private int beginIndex;
private int currentIndex;
private int endIndex;
private List<?> entities;
public PageDTO(final Page<?> page, final List<?> entities) {
this.entities = entities;
this.currentIndex = page.getNumber() + 1;
this.beginIndex = Math.max(1, currentIndex - 5);
this.endIndex = Math.min(beginIndex + 10, page.getTotalPages());
}
想知道是否有另一种有效的方法来做到这一点?
最佳答案
我知道这是一个老问题,但我遇到了同样的问题,我想为可能感兴趣的人提供一个可能的解决方案。以下是我发现的对我的代码有帮助的内容: https://github.com/pkainulainen/spring-data-jpa-examples/blob/master/query-methods/src/main/java/net/petrikainulainen/springdata/jpa/todo/TodoMapper.java
我还使用了 JpaRepository对数据进行分页,以便 DTO 页面具有相同的参数(页码、大小等)。这是我的存储库:
@Repository
public interface Repository extends JpaRepository<Entity, Integer> {
/**
* Name the method according to what query you need to execute
* e.g. findAll --> return all the rows that satisfy the following conditions,
* ByUsername --> username is a field in entity class,
* @param pageable: pagination is applied on the data.
* @return
*/
public Page<Entity> findAllByUsername(String username, Pageable pageable);
}
这是我进行映射的方法:
public Page<EntityDTO> findByUsername(String username, Pageable pageable){
Page<Entity> entityPage = entityRepository.findAllByUsername(username, pageable);
List<EntityDTO> dtos = mapper.entityToEntityDTOs(entityPage.getContent());
return new PageImpl<>(dtos, pageable, entityPage.getTotalElements());
}
还有我的Mapstruct映射器:
import org.mapstruct.factory.Mappers;
/**
* Mapper for converting entity to DTO.
*/
@Mapper(componentModel = "spring", uses = {})
public interface Mapper {
/**
* The interface declares a member INSTANCE, providing clients access to the mapper implementation,
* which is the file target\generated-sources\com\company\springapp\dto\mappers\MapperImpl.java
* (automatically generated when compiling the project).
*/
AuditMapper INSTANCE = Mappers.getMapper( Mapper.class );
/**
* Convert entity to DTO.
* Mappings are used to 'bind' entity fields to DTO fields (for the mapper's implementation).
* @param entity
* @return
*/
@Mappings({
@Mapping(source = "id", target = "id"),
@Mapping(source = "username", target = "dtoUsername"),
@Mapping(source = "action", target = "dtoAction")
})
public EntityDTO entityToEntityDTO(Entity entity);
/**
* Convert entities' list to DTOs' list.
* @param entities
* @return
*/
public List<EntityDTO> entitiesToEntityDTOs(List<Entity> entities);
}
关于java - 将 Page<Entity> 转换为 PageDTO<EntityDTO>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30644543/
我正在使用 spring 数据 JPA。 我的 Controller 如下所示 @RequestMapping(value = "/pages/{pageNumber}", method =
我是一名优秀的程序员,十分优秀!