zoukankan      html  css  js  c++  java
  • [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()
            );
        }

  • 相关阅读:
    BZOJ 1452 Count(二维树状数组)
    BZOJ 1407 Savage(拓展欧几里得)
    BZOJ 1415 聪聪和可可(期望DP)
    BZOJ 1406 密码箱(数论)
    最大流小结
    UVA6531Go up the ultras
    二分图小结
    Codeforces Round #243 (Div. 1)
    图论模板集合
    zoj3416 Balanced Number
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14141933.html
Copyright © 2011-2022 走看看