简体   繁体   English

Spring数据查询dsl如何添加order by?

[英]Spring data query dsl how to add order by?

I'm new to Querydsl, I'm working on Spring data and Querydsl to make a dynamic filter, I use the interface: QueryDslPredicateExecutor so I can filter data using different entity fields, now I want to add "order by" to my query based on a BooleanExpression .我是 Querydsl 的新手,我正在使用 Spring 数据和 Querydsl 来制作动态过滤器,我使用接口: QueryDslPredicateExecutor所以我可以使用不同的实体字段过滤数据,现在我想在我的查询中添加“order by”基于BooleanExpression

This my code:这是我的代码:

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

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

Then I called my respository interface using the query:然后我使用查询调用了我的存储库接口:

personInventoryRepository.findAll(query, pageable);

My question is how I can apply "order by" to my query object based on different fields on my entity?我的问题是如何根据实体上的不同字段将“order by”应用于查询对象?

Thanks all finally this solution work for me :最后感谢大家这个解决方案对我有用:

 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()));   

You can add sort to your page information:您可以为页面信息添加排序:

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

Add a method in your repository interface在您的存储库界面中添加一个方法

findByStatus(status)

Then use code block like below然后使用如下代码块

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

if your using spring boot 2.0.0 => then use method如果您使用 spring boot 2.0.0 => 然后使用方法

PageRequest.of(....)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM