【问题标题】:Spring data query dsl how to add order by?Spring数据查询dsl如何添加订单?
【发布时间】:2019-03-08 17:28:26
【问题描述】:

我是 Querydsl 的新手,我正在使用 Spring 数据和 Querydsl 来制作动态过滤器,我使用接口:QueryDslPredicateExecutor 所以我可以使用不同的实体字段过滤数据,现在我想要基于 BooleanExpression 将“order by”添加到我的查询中。

这是我的代码:

QPersonData _personInventory = QPersonData.personData;
BooleanBuilder query = new BooleanBuilder();

query.and(_personInventory.status.eq(status));

然后我使用查询调用了我的存储库接口:

personInventoryRepository.findAll(query, pageable);

我的问题是如何根据实体上的不同字段将“排序依据”应用于查询对象?

【问题讨论】:

    标签: java spring spring-data-jpa spring-data querydsl


    【解决方案1】:

    最后感谢所有这个解决方案对我有用:

     QPersonData _personInventory = QPersonData.personData;
     BooleanBuilder query = new BooleanBuilder(); 
    
     query.and(_personInventory .status.eq(status));
     personInventoryRepository.findAll(query,0, Integer.MAX_VALUE,new QSort(_personInventory.field1.asc(),_personInventory.field2.asc()));   
    

    【讨论】:

      【解决方案2】:

      您可以为您的页面信息添加排序:

       Sort sort = new Sort.Order(Sort.Direction.ASC,"filedname").nullsLast();
       PageRequest pageRequest = new PageRequest(pageNumber, pageSize, sort);
       personInventoryRepository.findAll(query,pageRequest); 
      

      【讨论】:

        【解决方案3】:

        在你的仓库界面中添加一个方法

        findByStatus(status)
        

        然后使用下面的代码块

        Pageable pageable = new PageRequest(offset, limit, Direction.DESC, "updatedAt");
        repository.findByStatus(status, pageable);
        

        如果您使用的是 spring boot 2.0.0 => 然后使用方法

        PageRequest.of(....)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-12
          • 2019-03-10
          • 1970-01-01
          • 2020-07-19
          • 1970-01-01
          • 2014-10-15
          • 1970-01-01
          • 2021-03-21
          相关资源
          最近更新 更多