Mih*_* L. 5 java spring querydsl spring-data-jpa spring-boot
我想使用org.springframework.data.domain.Pageable
withcom.querydsl.jpa.impl.JPAQuery
有没有办法或者有可靠的解决方法如何使 Pageable 与 JPAQuery 一起工作?
所以我发现为什么要让他们一起工作发布完整的示例:
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.persistence.EntityManager;
public class Example {
public void example(Pageable pageable, EntityManager em){
QCustomer qCustomer = QCustomer.customer;
JPAQuery<?> query = new JPAQuery<>(em);
query.from(qCustomer);
BooleanBuilder builder = new BooleanBuilder();
builder.and(qCustomer.email.likeIgnoreCase("c@m.c"));
JPAQuery<?> where = query.where(builder);
// This for loop is making it work with Pagable
query.offset(pageable.getOffset());
query.limit(pageable.getPageSize());
PathBuilder<Customer> entityPath = new PathBuilder<>(Customer.class, "customer");
for (Sort.Order order : pageable.getSort()) {
PathBuilder<Object> path = entityPath.get(order.getProperty());
query.orderBy(new OrderSpecifier(Order.valueOf(order.getDirection().name()), path));
}
List<Customer> resultList = query.createQuery().getResultList();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3349 次 |
最近记录: |