用spring和querydsl排序

时间:2018-07-10 00:18:22

标签: spring-boot querydsl

我正在尝试在@RepositoryRestResource中进行排序,在这里我创建了几个querydsl接口的自定义查询,但似乎丢失了一些东西。分页有效,但是您不能对一个以上单词(shippedQty)的字段进行排序。在其他字段上排序效果很好。这是一个PagingAndSortingRepository错误,还是我需要做其他事情或多字字段?

@RepositoryRestResource(path = "/report", collectionResourceRel = "report", itemResourceRel = "report")
public interface ReportRepository extends PagingAndSortingRepository<Report, Long>, QueryDslPredicateExecutor<Report>,
        QuerydslBinderCustomizer<QReport> {

    @Override
    default void customize(QuerydslBindings bindings, QReport report) {
        bindings.including(
                report.description,
                report.item,
                report.program,
                report.shippedQty,
        );
        bindings.excludeUnlistedProperties(true);

        SingleValueBinding<NumberPath<Integer>, Integer> numberPathContains = (path, value) -> path.stringValue().contains(value.toString());

        bindings.bind(firstFill.description).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.item).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.program).first(StringPath::containsIgnoreCase);
        bindings.bind(firstFill.shippedQty).as("shipped_qty").first(numberPathContains);

 }
}

此排序正确:

http://localhost:8080/api/v1/report?page=0&size=5&sort=description,asc

这不是:

http://localhost:8080/api/v1/report?page=0&size=5&sort=shipped_qty,asc

1 个答案:

答案 0 :(得分:1)

我自己遇到了这个问题。事实证明,排序不使用QueryDSL存储库绑定别名,而是使用“ Q”实体路径的名称。