使用可分页的自定义查询。 Spring 数据 MongoDB ( 1.10.9 )
Custom Query with Pageable. Spring Data MongoDB ( 1.10.9 )
我已经创建了一个自定义存储库:
// 我的仓库:
@SuppressWarnings("unused")
@Repository
public interface PrlRepository extends MongoRepository<Prl, String>, PrlRepositoryCustom {
// 我的界面:
public interface PrlRepositoryCustom {
List<Prl> find(FilterPrlDTO filterPrlDTO);
}
//我的实现
@Override
public List<Prl> find(FilterPrlDTO filterPrlDTO) {
List<Criteria> andCriteria = new ArrayList<>();
if (filterPrlDTO.getInicioCaduca() != null) {
andCriteria.add(Criteria.where("caducidad").gte(filterPrlDTO.getInicioCaduca()));
}
if (filterPrlDTO.getFinCaduca() != null) {
andCriteria.add(Criteria.where("caducidad").lte(filterPrlDTO.getFinCaduca()));
}
if (filterPrlDTO.getActivo() != null) {
andCriteria.add(Criteria.where("activo").is(filterPrlDTO.getActivo()));
}
Criteria orCriteria = new Criteria().andOperator(andCriteria.toArray(new Criteria[andCriteria.size()]));
return mongoOperations.find(new Query().addCriteria(orCriteria), Prl.class);
}
它工作正常,但我需要它是可分页的。
谁能帮我实现一下?我一直在查看论坛和文档,但我没有看到任何我可以提供的东西
非常感谢。
MongoRepository 实际上实现了 PagingAndSortingRepository,它允许您实现分页。您可以将 Pageable 请求与查询 class 一起传递,这里是一个示例如何做到这一点,
Pageable pageable = new PageRequest(pageNumber, dataSize);
Query query = new Query();
public Page<T> findAll(Query query, Pageable pageable) {
Long count = count(); //Issue a count query here for the collection
List<T> list = findAll(query.with(pageable));
return new PageImpl<T>(list, pageable, count);
}
使用大过滤器进行搜索的结构,它将根据搜索参数输入 Criteria 并分页结果:
public Page<AccionOportunidad> filter(Pageable pageable) {
Query query = new Query();
// criterias
query.addCriteria(Criteria.where("id").in("xxx"));
query.with(pageable);
List<AccionOportunidad> list = mongoOperations.find(query, AccionOportunidad.class);
return PageableExecutionUtils.getPage(list, pageable,
() -> mongoOperations.count(query, AccionOportunidad.class));
}
我已经创建了一个自定义存储库:
// 我的仓库:
@SuppressWarnings("unused")
@Repository
public interface PrlRepository extends MongoRepository<Prl, String>, PrlRepositoryCustom {
// 我的界面:
public interface PrlRepositoryCustom {
List<Prl> find(FilterPrlDTO filterPrlDTO);
}
//我的实现
@Override
public List<Prl> find(FilterPrlDTO filterPrlDTO) {
List<Criteria> andCriteria = new ArrayList<>();
if (filterPrlDTO.getInicioCaduca() != null) {
andCriteria.add(Criteria.where("caducidad").gte(filterPrlDTO.getInicioCaduca()));
}
if (filterPrlDTO.getFinCaduca() != null) {
andCriteria.add(Criteria.where("caducidad").lte(filterPrlDTO.getFinCaduca()));
}
if (filterPrlDTO.getActivo() != null) {
andCriteria.add(Criteria.where("activo").is(filterPrlDTO.getActivo()));
}
Criteria orCriteria = new Criteria().andOperator(andCriteria.toArray(new Criteria[andCriteria.size()]));
return mongoOperations.find(new Query().addCriteria(orCriteria), Prl.class);
}
它工作正常,但我需要它是可分页的。
谁能帮我实现一下?我一直在查看论坛和文档,但我没有看到任何我可以提供的东西
非常感谢。
MongoRepository 实际上实现了 PagingAndSortingRepository,它允许您实现分页。您可以将 Pageable 请求与查询 class 一起传递,这里是一个示例如何做到这一点,
Pageable pageable = new PageRequest(pageNumber, dataSize);
Query query = new Query();
public Page<T> findAll(Query query, Pageable pageable) {
Long count = count(); //Issue a count query here for the collection
List<T> list = findAll(query.with(pageable));
return new PageImpl<T>(list, pageable, count);
}
使用大过滤器进行搜索的结构,它将根据搜索参数输入 Criteria 并分页结果:
public Page<AccionOportunidad> filter(Pageable pageable) {
Query query = new Query();
// criterias
query.addCriteria(Criteria.where("id").in("xxx"));
query.with(pageable);
List<AccionOportunidad> list = mongoOperations.find(query, AccionOportunidad.class);
return PageableExecutionUtils.getPage(list, pageable,
() -> mongoOperations.count(query, AccionOportunidad.class));
}