概述
原理都是取出Page包装的对象,然后实现对象之间的互传,在返回Page,这里介绍几种实现方式
1. 这个mapper是基于mapStruct,常用来实现Model,DTO的互转,
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
引 Jar,pom文件引Jar,还有build插件,Maven 编译等,稍微麻烦些
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs>
</configuration>
</plugin>
以下是demo示例
public Page<CustomerDTO> page2(){
PageRequest pageRequest = PageRequest.of(0, 20, Sort.Direction.DESC,"customerNumber");
Page<CustomersEntity> all = customerRepo.findAll(pageRequest);
Page<CustomerDTO> map = all.map(e -> customerMapper.toCustomerDto(e));
return map;
}
2. 基于Json实现对象的互转,这里我用的是Hutool包,(注意这里如果涉及时间类型的字段映射,可能会转换错误)
public Page<CustomerDTO> page3(){
PageRequest pageRequest = PageRequest.of(0, 20, Sort.Direction.DESC,"customerNumber");
Page<CustomersEntity> all = customerRepo.findAll(pageRequest);
Page<CustomerDTO> map = all.map(e -> JSONUtil.toBean(JSONUtil.toJsonStr(e),CustomerDTO.class));
return map;
}
注意,Hutool包的JsonUtil在转换对象的时候,有时候会因为时间类型(LocalDateTime)的字段,转换错误,第一种Mapper方式还没试
3. 使用构造方法实现对象的转换,在V0中定义如下的构造方法即可,注意参数
public MoveRecordVo(MoveInfo moveInfo) {
this.assetId = moveInfo.getAssetId();
this.assetName = moveInfo.getAssetName();
this.startTime = moveInfo.getStartTime();
this.endTime = moveInfo.getEndTime();
this.moveTime = null;
this.moveStatus = moveInfo.getMoveStatus();
this.remarks = moveInfo.getRemarks();
this.isOperate = null;
}
这样在转换对象的时候,就很方便了
Page<MoveInfo> all = moveInfoRepository.findAll(pageRequest);
Page<MoveRecordVo> map = all.map(e -> new MoveRecordVo(e));
4. 还有一种方法,稍微麻烦一点,就是先把Page转成List,再根据之前的分页参数(JPA,根据前端传参,计算好的分页参数Pageable)返回封装Page,代码如下,不想细整理了
public Page<TranscodeRecordVo> findAll(TranscodeDto transcodeDto, int page, int rows, String sord, String sidx) {
if (StrUtil.isBlankIfStr(sidx)) {
sidx = "transcodeId";
}
PageRequest pageRequest = PageRequest.of(page - 1, rows, Sort.by(Sort.Order.asc(sidx), Sort.Order.desc("collectTime")));
Page<Transcode> all = transcodeRepository.findAll((Specification<Transcode>) (root, query, cb) -> {
List<Predicate> predicates = Lists.newArrayList();
predicates.add(cb.equal(root.get("groupId"), transcodeDto.getGroupId()));
if (!StrUtil.isBlankIfStr(transcodeDto.getAssetId())) {
predicates.add(cb.equal(root.get("assetId"), transcodeDto.getAssetId()));
}
if (!StrUtil.isBlankIfStr(transcodeDto.getAssetName())) {
predicates.add(cb.like(root.get("assetName"), "%" + transcodeDto.getAssetName() + "%"));
}
if (!StrUtil.isBlankIfStr(transcodeDto.getTranscodeStatus())) {
predicates.add(cb.equal(root.get("transcodeStatus"), transcodeDto.getTranscodeStatus()));
}
if (!StrUtil.isBlankIfStr(transcodeDto.getFromStartTime())) {
predicates.add(cb.greaterThanOrEqualTo(root.get("endTime"), transcodeDto.getFromStartTime()));
}
if (!StrUtil.isBlankIfStr(transcodeDto.getToEndTime())) {
predicates.add(cb.lessThanOrEqualTo(root.get("endTime"), transcodeDto.getToEndTime()));
}
return cb.and(predicates.toArray(new Predicate[0]));
}, pageRequest);
ArrayList<TranscodeRecordVo> list = Lists.newArrayList();
all.getContent().forEach(e -> {
TranscodeRecordVo vo = new TranscodeRecordVo();
BeanUtil.copyProperties(e, vo);
list.add(vo);
});
list.forEach(e -> {
if (NodeResultEnum.TranscodeStatus.STARTED.getKey().equals(e.getTranscodeStatus())) {
e.setTransTime(DateUtil.formatBetween(ChronoUnit.MILLIS.between(e.getStartTime(), LocalDateTime.now()), BetweenFormater.Level.SECOND));
} else {
e.setTransTime(DateUtil.formatBetween(ChronoUnit.MILLIS.between(e.getStartTime(), e.getEndTime()), BetweenFormater.Level.SECOND));
}
if (NodeResultEnum.TranscodeStatus.FAILED.getKey().equals(e.getTranscodeStatus())) {
e.setIsOperate(YesOrNoEnum.YES.getCode());
} else {
e.setIsOperate(YesOrNoEnum.NO.getCode());
}
});
return new PageImpl<>(list, all.getPageable(), all.getTotalElements());
}
这里是我业务中的代码,关于List,Page对象,稍微复杂点的操作
最后
以上就是坦率仙人掌为你收集整理的Page
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复