Mongodb系列- spring-data-mongodb使用MongoTemplate实现分页查询
在用spring-data-mongodb框架开发的过程中,需要实现分页查询,就百度了下,没找到满意的又google了下,找到了思路.
在spring-data-mongodb 官方文档中,建议你使用
PagingAndSortingRepository来实现分页,但是我是真的不喜欢这个设计啊!!
用方法名来映射查询语句,框架会自动生成执行代码,还为此定义了一套语法,举个例子:
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();
}
这个接口类只定义了接口,并不需要实现,因为SDM框架(spring-data-mongodb简称,以下都使用简称)会帮你生成代码..
findByAgeBetween(int ageGT, int ageLT);-> 就是where ageGT <age and age <ageLT;
刚开始可能感觉很简单,但是一旦字段多了查询条件复杂了! 你根本不知道自己在写什么!别人看你的代码一长串方法名,也是直接懵逼的..
而 查出来的许多分页查询也是直接使用的
PagingAndSortingRepository这个接口,自动生成...非常不喜欢...就去查怎么使用MongoTemplate实现...
先下班....放假回来补上..哈哈
庆祝五一上班,把没写的写完...
使用MongoTemplate实现分页
@Repository("deviceStatusRepository")
public class DeviceStatusRepository {
@Autowired
private MongoOperations mongoOperations;
/** * 分页查询 */ public PageImpl<DeviceStatusItem> pageDeviceStatusItemByDeviceSerial(String deviceSerial, String collectionName, int pageIndex, int pageSize) { Query query = Query.query( Criteria.where(CONSTS.DEVICE_SERIAL_FIELD).is(deviceSerial)); // 每页五个 Pageable pageable = new PageRequest(pageIndex, pageSize); // get 5 profiles on a page query.with(pageable); // 排序 query.with(new Sort(Direction.ASC, CONSTS.DEVICE_SERIAL_FIELD, CONSTS.DOMAINID_FIELD)); // 查询总数 int count = (int) mongoOperations.count(query, DeviceStatusItem.class, collectionName); List<DeviceStatusItem> items = mongoOperations.find(query, DeviceStatusItem.class, collectionName); // System.out.println("stories:" + stories.size() + " count:" + count); return (PageImpl<DeviceStatusItem>) PageableExecutionUtils.getPage(items, pageable, () -> count); }
}
解析:
MongoOperations 是MongoTemplate的接口,它的具体实现就是MongoTemplate,所以这里使用MongoTemplate或MongoOperations 都可以.
1. 创建PageRequest 对象,这是SDM框架提供的现成的分页请求类.构造函数很简单:
// page:第几页, size:每页的大小
public PageRequest(int page, int size) {
this(page, size, null); }
2. 构建Query 查询条件.我这里先是指定了根据序列号查询,然后设置分页请求 query.with(pageable);最后设置结果的排序.
3. 使用SDM框架自带的工具类PageableExecutionUtils 返回PageImpl .这里的PageableExecutionUtils 和PageImpl 其实都可以自己实现,你可以打开PageImpl 看一下代码很简单,就是对查询结果封装了下,方便数据的返回.
调用方法:
// 序列号 String deviceSerial="123456"; //集合的名字,就相当于表名 String cllectionName="device"; //返回第几页 int pageIndex = 0; //每页的大小 int pageSize = 10; PageImpl<DeviceStatusItem> pageImpl = deviceStatusRepository .pageDeviceStatusItemByDeviceSerial(deviceSerial, collectionName, pageIndex, pageSize); System.out.println("list:" + pageImpl.getContent() + " number:" + pageImpl.getNumber() + " size:" + pageImpl.getSize() + " pages:" + pageImpl.getTotalPages() + " TotalElements:" + pageImpl.getTotalElements());
解析: 这个PageImpl 方法名很清晰了.
查询的结果集: pageImpl.getContent(),
当前页是第几个: pageImpl.getNumber()
当前页的大小: pageImpl.getSize()
一共多少页: pageImpl.getTotalPages()
一共多少条记录: pageImpl.getTotalElements()
参考:
https://stackoverflow.com/questions/27296533/spring-custom-query-with-pageable?rq=1
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询(转)
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- 在Spring Boot中使用Spring-data-jpa实现分页查询
- Spring MVC结合Spring Data JPA实现按条件查询和分页
- 在SPRING DATA MONGODB中使用聚合统计查询
- 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现
- 【spring data jpa】带有条件的查询后分页和不带条件查询后分页实现
- EasyUi+Spring Data 实现按条件分页查询
- 分布式缓存技术redis学习系列(五)——spring-data-redis与JedisPool的区别、使用ShardedJedisPool与spring集成的实现及一致性哈希分析
- 使用Spring Data Jpa 遇到 数据库字段以 from 开头,分页查询失败的问题