亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用“可分頁”進行排序時出現重復項

使用“可分頁”進行排序時出現重復項

滄海一幻覺 2022-09-14 10:43:27
控制器:@RequestMapping(path = "/serviceslist", method = RequestMethod.GET)    public Page<ServiceResponse> getServicesList(            @RequestParam(defaultValue = "0") Integer page,            @RequestParam(defaultValue = "10") Integer size,            @RequestParam(required = false) String search,            @RequestParam(required = false) String name,            @RequestParam(required = false) String jobs,            @RequestParam(required = false) Boolean needsPatrol,            @RequestParam(defaultValue = "createTime") String sort,            @RequestParam(defaultValue = "asc") String sortDir    ) {        ServiceListRequest request = new ServiceListRequest(search, name, jobs, needsPatrol);        Sort.Direction direction;        if (sortDir.equals("asc")) {            direction = Sort.Direction.ASC;        } else {            direction = Sort.Direction.DESC;        }        return serviceService.getServicesList(request, of(page, size, direction, sort))                .map(ServiceResponse::new);    }服務:@Entity@Data@Table(name = "service")public class Service {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(unique = true, nullable = false, columnDefinition = "serial")    private Long id;    @Column(name = "name", nullable = false)    private String name;    @Column(name = "price", nullable = false)    private float price;    @Column(name = "season", nullable = false, columnDefinition = "season_enum")    @Enumerated(EnumType.STRING)    @Type(type = "pgsql_enum")    private SeasonEnum season;    )   }一個服務可以有許多作業。我試圖實現的是按作業中的第一個項目對 ASC/DESC 進行排序,但目前,如果服務有 4 個作業,則在排序后使用以下方法進行排序:localhost:8080/serviceslist?sort=jobs&sortDir=asc它給了我4次服務,但我需要它與眾不同??煞猪撌欠裼修k法刪除重復項并解決我的排序問題?按名稱和東西排序工作正常,只是工作部分壞了:/編輯21.04.2019:如果我之前不太清楚,我很抱歉。理想的結果是按字母順序對第一個作業進行排序,因為作業是按完成時間的順序排列的。這有什么可能嗎?提前致謝!
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

您必須設置不同的,并檢查查詢返回類型:


將調用長分頁計數查詢


Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, cq, cb) -> {

    if(!Long.class.isAssignableFrom(cq.getResultType())) {

        cq.distinct(true);

    }

    //else {

         //create a query for count case if needed 

    //}

    return null;

};

編輯的答案:


在這種情況下,考慮到 updateTime 可能指示作業排序,我建議您執行以下操作:


Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, cq, cb) -> {

    if(!Long.class.isAssignableFrom(cq.getResultType())) {


        if(sort.contains("jobs")) {

            Join<Service, Job> jobs = root.join("jobs");

            //check for asc or desc

            cq.orderBy(cb.asc(jobs.get("updateTime")));

        }

        cq.distinct(true);

    }

    //else {

         //create a query for count case if needed 

    //}

    return null;

};

干杯


查看完整回答
反對 回復 2022-09-14
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

您需要有一個唯一的訂單標準。如果不確定,請添加“id”作為最低優先級的排序條件。

問題是:如果未指定到記錄級別,則數據庫排序未定義。即:如果您指定排序但保留最后一位未定義(即,如果兩行滿足相同的排序條件),您將遇到即使一行中的兩個相同查詢也會返回相同的結果,但順序不同。


查看完整回答
反對 回復 2022-09-14
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

你可以做這樣的事情


public Page<com.bitweb.syda.data.entity.service.Service> getServicesList(ServiceListRequest request, Pageable pageable) {

        Specification<com.bitweb.syda.data.entity.service.Service> spec = (root, query, builder) -> {

            //you can do any check here if you want with the join and check all the search parameters here if you want

            //Join<Object, Object> jobs = root.join("jobs");

            // also set query to get distinc values

            query.distinct(true);

            return null;

        };


        if (request.getSearch() != null) spec = spec.and(search(request.getSearch()));

        if (request.getName() != null) spec = spec.and(name(request.getName()));

        if (request.getJobs() != null) spec = spec.and(hasJobs(request.getJobs()));

        if (request.getNeedsPatrol() != null) spec = spec.and(needsPatrol(request.getNeedsPatrol()));


        return serviceRepository.findAll(spec, pageable);

    }


查看完整回答
反對 回復 2022-09-14
  • 3 回答
  • 0 關注
  • 86 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號