Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the multi-attribute sorting method in spring data JPA

Shulou Source: shulou.com Published: 2022-06-01 12:49:53 10月13日 Update

In this article Xiaobian for you to introduce in detail "what is the multi-attribute sorting method in spring data JPA", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the multi-attribute sorting method in spring data JPA" can help you solve your doubts.

Multi-attribute sorting of spring data JPA

I would like to introduce one of the ways I have used:

First step, lead package import org.springframework.data.domain.Sort;import org.springframework.data.domain.Sort.Order; second step, service method code @ Override public Page findAll (Integer pageNum, Integer pageSize, TurnOverRecord turnOverRecord) {/ / multiple attributes sort / / first by isTurnOver ascending order, and then by turnOverTime ascending order List

< Order>

Orders=new ArrayList

< Order>

(); orders.add (new Order (Sort.Direction. ASC, "isTurnOver"); orders.add (new Order (Sort.Direction. ASC, "turnOverTime"); Pageable pageable= new PageRequest (pageNum-1, pageSize, new Sort (orders)); Specification specification = new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder cb) {Predicate predicate = cb.conjunction () / / single attribute sort / / query.orderBy (cb.asc (root.get ("isTurnOver"); / / query.orderBy (cb.desc (root.get ("id"); / / query.groupBy (root.get ("id")); query.distinct (true) / / basic condition predicate.getExpressions () .add (cb.equal (root.get ("flag"), 1); if (turnOverRecord! = null) {other filter criteria} return predicate;}}; return turnOverRecordDao.findAll (specification, pageable) } spring data JPA scheduling problem (order by)

1: you can pass Pageable objects, where Pageable can contain Sort parameters

2: you can pass Sort objects

3: you can add order by manually by writing sql/hql statements in @ Query

There is no need to restart eclipse after modification of the first and second methods

The third way, although intuitively the simplest, requires a restart of eclipse.

After reading this, the article "what is the multi-attribute sorting method in spring data JPA" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Tags: Sorting properties methods articles methods content ascending order objects conditions appropriate from small to large no small code parameters fundamentals ideas used manual new knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Shulou Information vpn NVidia