• [Java Spring MVC] Paging and sorting DTOs


    Repo:

    package com.example.ec.repo;
    
    import com.example.ec.domain.TourRating;
    import com.example.ec.domain.TourRatingPk;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.Pageable;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
    
    import java.util.List;
    import java.util.Optional;
    
    @RepositoryRestResource(exported = false)
    public interface TourRatingRepository extends CrudRepository<TourRating, TourRatingPk> {
        List<TourRating> findByPkTourId(Integer tourId);
    
        Optional<TourRating> findByPkTourIdAndPkCustomerId(Integer tourId, Integer customerId);
    
        Page<TourRating> findByPkTourId(Integer tourId, Pageable pageable);
    }

    Controller:

        @GetMapping
        public Page<RatingDto> getAllRatingsForTour(@PathVariable(value = "tourId") int tourId, Pageable pageable) {
            verifyTour(tourId);
            Page<TourRating> ratings = tourRatingRepository.findByPkTourId(tourId, pageable);
            return new PageImpl<>(
                    ratings.get()
                        .map(RatingDto::new)
                        .collect(Collectors.toList()),
                    pageable,
                    ratings.getTotalElements()
            );
        }

  • 相关阅读:
    git 更改项目名称
    C语言 函数和程序结构 宏替换#define
    git commit
    git patch
    CortexM3/4 一些调试技巧
    PendSV和SYSTICK
    TencentOS tiny 中断嵌套
    smp raw_spinlock_t
    20212022年寒假学习进度15
    20212022年寒假学习进度22
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14141933.html
Copyright © 2020-2023  润新知