Spring Data JPA uses sort to sort (using sort) (GO)

Source: Internet
Author: User

Through the previous section of the study, we learned how to use @query annotations to achieve a flexible query. In the example in the previous section, I also tried to give a simple sort, with JPQL statements and native SQL. This implementation, although to some extent can be applied, but not enough flexibility, so in conjunction with @query annotations, we can use sort to sort the results.

1. Adding methods within Customerrepository

/** * A parameter that matches two fields * @param name2 * @param sort Specifies the parameter of the sort, which can be adjusted as needed * @return * Here the value of param and =: The following parameter matches, but does not need to correspond to the method name corresponding to the parameter value * */@Qu Ery ("Select C from Customer C where c.firstname=:name or c.lastname=:name") list<customer> findByName4 (@Param (" Name ") String name2,sort Sort);

  

method as always, is declarative, but on the basis of the original method, plus Sort (org.springframework.data.domain.Sort) as a parameter.

2. Test in Customercontroller

/** * @Query Annotated query, * Specify parameters with @param, match FirstName and LastName*/@RequestMapping ("/findbyname") Public voidfindByName4 () {//in reverse order of IDSystem. out. println ("Create a Sort object directly by sorting methods and property names"); Sort Sort=NewSort (Sort.Direction.DESC,"ID"); List<Customer> result = Repository.findbyname4 ("Bauer", sort);  for(Customer customer:result) {System. out. println (Customer.tostring ()); } System. out. println ("-------------------------------------------"); //in reverse order of IDSystem. out. println ("To create a sort object from a Sort.order object"); Sort Sortx=NewSort (NewSort.order (Sort.Direction.DESC,"ID")); List<Customer> Resultx = Repository.findbyname4 ("Bauer", sort);  for(Customer customer:result) {System. out. println (Customer.tostring ()); } System. out. println ("-------------------------------------------"); System. out. println ("To create a sort object by sorting methods and properties list"); List<String> sortproperties =NewArraylist<>(); Sortproperties.add ("ID"); Sortproperties.add ("FirstName"); Sort Sort2=NewSort (sort.direction.desc,sortproperties); List<Customer> result2 = Repository.findbyname4 ("Bauer", Sort2);  for(Customer customer:result2) {System. out. println (Customer.tostring ()); } System. out. println ("-------------------------------------------"); System. out. println ("Create a Sort object by creating a collection of Sort.order objects"); List<Sort.Order> orders =NewArraylist<>(); Orders.add (NewSort.order (Sort.Direction.DESC,"ID")); Orders.add (NewSort.order (Sort.Direction.ASC,"FirstName")); List<Customer> RESULT3 = Repository.findbyname4 ("Bauer",NewSort (orders));  for(Customer customer:result3) {System. out. println (Customer.tostring ()); } System. out. println ("-------------------------------------------");}

A total of four sorting methods are listed here:

1) Create a Sort object directly for sorting a single attribute

2) Create sort object from Sort.order object, suitable for sorting single attribute

3) Create a Sort object from the list collection of attributes, suitable for sorting on multiple properties, taking the same sort

4) Create sort object by Sort.order object's list collection, suitable for all situations, easy to set sort

Corresponds to our usage scenario to choose how to create a sort object.

Note that this does not list all the sort usage, but also ignore the case, use Jpasort.unsafe, aggregate functions and so on to sort, the property value of the query is the attribute name of the entity, not the database field, you have to notice!!

For more usage, please refer to source code: Http://www.icnws.com/wp-content/uploads/2017/06/Sort.java_.txt

Reference:

Official document, http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

Demo,https://github.com/icnws/spring-data-jpa-demo

Spring Data JPA uses sort to sort (using sort) (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.