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