Page對象互轉

都是基於map方法,介紹 2 種實現方式

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 編譯等,稍微麻煩些

 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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章