MongoDB Series-Use SPRING-DATA-MONGODB for paging queries

Source: Internet
Author: User

In the process of using SPRING-DATA-MONGODB framework development, the need to achieve paged query, Baidu, did not find the satisfaction of Google again, found the idea.

In Spring-data-mongodb official documents, it is recommended that you use PagingAndSortingRepository to implement paging, but I really do not like this design Ah!!

Using the method name to map the query statement, the framework automatically generates the execution code and defines a set of syntax for this, for example:

Public interface Userrepository extends Mongorepository<user, string>, querydslpredicateexecutor<user> {
@Query ("{' name ':? 0}")
List<user> findusersbyname (String name);

@Query ("{' age ': {$gt:? 0, $lt:? 1}}")
list<user> findusersbyagebetween (int agegt, int agelt);

List<user> findbyname (String name);

List<user> Findbynamelikeorderbyageasc (String name);

list<user> findbyagebetween (int agegt, int agelt);

@Query (value = "{}", fields = "{name:1}")
List<user> Findnameandid ();

@Query (value = "{}", fields = "{_id:0}")
List<user> Findnameandageexcludeid ();
}

This interface class only defines the interface and does not need to be implemented, because the SDM framework (SPRING-DATA-MONGODB, which uses short abbreviations) will help you generate the code:

Findbyagebetween (int agegt, int agelt);-> is where Agegt <age and age <ageLT;

It may feel simple at first, but once the fields are more complex, the query conditions are complicated! You don't even know what you're writing! People look at your code a long list of method names, is also directly ignorant of the force.

And many of the paged query is also directly used by PagingAndSortingRepository this interface, automatically generated ... I don't like it very much. Just go and find out how to use mongotemplate to achieve ...

Get off work first .... Holiday back to fill up. Ha ha

MongoDB Series-Use SPRING-DATA-MONGODB for paging queries

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.