mongodb多条件分页查询的三种方法(转)
一、使用limit和skip进行分页查询
public List<User> pageList(int pageNum ,int pageSize){ List<User> userList = new ArrayList<>(); Mongo mg = new Mongo(); DB db = mg.getDB("data"); DBCollection coll = db.getCollection("t_user"); DBCursor limit = coll.find().skip((pageNum-1)*pageSize).sort(new BasicDBObject()).limit(pageSize); while (limit.hasNext()){ userList.add(parse(new User(),limit.next())); } return userList; } private User parse(User user,DBObject obj){ user.setAge((int) obj.get("age")); user.setName((String)obj.get("name")); user.setPwd((String)obj.get("pwd")); return user; } //查询结果 1,2 [ { "id": null, "name": "ljl", "pwd": "123456", "age": 24 }, { "id": null, "name": "lsr", "pwd": "123456", "age": 18 } ]
通过skip和limit方法可以简单的实现分页操作,但是如果数据量特别巨大的时候,会出现性能的问题,建议不使用!
二、通过原生的方法实现条件查询、分页和排序
public Page<User> getPageByOriginalFunction(int age,int pageNUmber,int pageSize){ //查询条件,可以传递多个查询条件 User user = new User(); user.setAge(age); Example<User> example = Example.of(user); //分页条件 //Pageable pageable = new PageRequest(pageNUmber,pageSize); Pageable pageable = PageRequest.of(pageNUmber,pageSize); return secondRepository.findAll(example,pageable); } //查询结果 { "content": [ { "id": "5cfb69ee4332ce07b864d12e", "name": "lsr", "pwd": "123456", "age": 18 } ], "pageable": { "sort": { "sorted": false, "unsorted": true }, "offset": 0, "pageSize": 2, "pageNumber": 0, "unpaged": false, "paged": true }, "last": true, "totalPages": 1, "totalElements": 1, "number": 0, "size": 2, "sort": { "sorted": false, "unsorted": true }, "first": true, "numberOfElements": 1 }
三、通过实现Pageable接口,自定义
1.创建自定义分页类,实现Pageable接口
import lombok.AllArgsConstructor; import lombok.NoArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import javax.validation.constraints.Min; @NoArgsConstructor @AllArgsConstructor public class SpringDataPageAble implements Pageable { @Min(1) private Integer pageNumber = 1; @Min(1) private Integer pageSize = 10; private Sort sort; public void setSort(Sort sort) { this.sort = sort; } // 当前页面 @Override public int getPageNumber() { return this.pageNumber; } // 每一页显示的条数 @Override public int getPageSize() { return getPagesize(); } // 第二页所需要增加的数量 @Override public long getOffset() { return (getPageNumber() - 1) * getPagesize(); } @Override public Sort getSort() { return sort; } public void setPagenumber(Integer pagenumber) { this.pageNumber = pageNumber; } public Integer getPagesize() { return this.pageSize; } public void setPagesize(Integer pagesize) { this.pageSize = pagesize; } @Override public Pageable next() { return null; } @Override public Pageable previousOrFirst() { return null; } @Override public Pageable first() { return null; } @Override public boolean hasPrevious() { return false; } }
2.在repository层定义分页方法
import com.tedu.huawei.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.repository.MongoRepository; public interface UserFirstRepository extends MongoRepository<User,String> { Page<User> getUserByAgeGreaterThan(int age, Pageable pageAble); }
3.service层调用方法
public Page<User> getUserByAgeGraterThan(int age, int pageNumber,int pageSize){ SpringDataPageAble springDataPageAble = new SpringDataPageAble(pageNumber,pageSize,new Sort(Sort.Direction.ASC,"age")); return firstRepository.getUserByAgeGreaterThan(age,springDataPageAble); }
查询结果显示
{ "content": [ { "id": "5cfb66114332ce07b864d12d", "name": "lsr", "pwd": "123456", "age": 18 }, { "id": "5cfb85084332ce4ffca97907", "name": "panzi", "pwd": "654321", "age": 24 } ], "pageable": { "pageNumber": 1, "pageSize": 2, "sort": { "sorted": true, "unsorted": false }, "offset": 0, "pagesize": 2, "unpaged": false, "paged": true }, "last": true, "totalPages": 1, "totalElements": 2, "number": 1, "size": 2, "sort": { "sorted": true, "unsorted": false }, "first": false, "numberOfElements": 2 }
四、总结
第一种方式实现简单方便,但是不适用于大数据量。第二种分页是原生的方法,不需要做额外的处理,但是查询条件单一,不能设置大于等于或者在某某之间,以及模糊查询有很大的限制。第三种方式实现也很简单,但是更加开放和使用性能好。
————————————————
版权声明:本文为CSDN博主「笑不语」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43935907/article/details/91354738