How can I convert a spring data Sort to a querydsl OrderSpecifier?

前端 未结 4 808
有刺的猬
有刺的猬 2021-02-04 06:23

This is basically the opposite of this: How to do a paged QueryDSL query with Spring JPA?

This is for a custom query for which i can\'t use any of the findAll() methods.

4条回答
  •  旧时难觅i
    2021-02-04 07:14

    private OrderSpecifier[] getSortedColumn(Sort sorts){   
        return sorts.toList().stream().map(x ->{
            Order order = x.getDirection().name() == "ASC"? Order.ASC : Order.DESC;
            SimplePath filedPath = Expressions.path(Object.class, Qobject, x.getProperty());
            return new OrderSpecifier(order, filedPath);
        }).toArray(OrderSpecifier[]::new);
    }
    
    

    then you can use like this:

      .where(...), 
      .orderBy(getSortedColumn(pageable.getSort()))
    

    提交回复
    热议问题