mongdb分页 加多条件查询 完美好用

步骤一
@Override
public PageInfo paginationQuery(com.dearho.core.Page p, OrderQuery orderQuery, Class clazz) {
List orders = new ArrayList();

    //排序

// orders.add(new Order(Sort.Direction.DESC, “age”));
// orders.add(new Order(Sort.Direction.ASC, “num”));

    List list = new ArrayList();

    Criteria c1 = Criteria.where("isDel").is(false);
    list.add(c1);

    //订单编号
    if(StringUtil.isNotEmpty(orderQuery.getOrderNo())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getOrderNo()) + ".*");
        Criteria c2 =  Criteria.where("orderNo").regex(pattern);
        list.add(c2);
    }
    //司机手机号
    if(StringUtils.isNotEmpty(orderQuery.getDriverPhone())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getDriverPhone()) + ".*");
        Criteria c3 =  Criteria.where("driverPhone").regex(pattern);
        list.add(c3);
    }
    //车牌号
    if(StringUtils.isNotEmpty(orderQuery.getBusNumber())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getBusNumber()) + ".*");
        Criteria c4 =  Criteria.where("busNumber").regex(pattern);
        list.add(c4);
    }
    //乘客手机号
    if(StringUtils.isNotEmpty(orderQuery.getUserMobile())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getUserMobile()) + ".*");
        Criteria c5 =  Criteria.where("userMobile").regex(pattern);
        list.add(c5);
    }
    //订单状态
    if(StringUtils.isNotEmpty(orderQuery.getState())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getState()) + ".*");
        Criteria c6 =  Criteria.where("state").regex(pattern);
        list.add(c6);
    }
    //运营平台
    if(StringUtils.isNotEmpty(orderQuery.getPlatform())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getPlatform()) + ".*");
        Criteria c6 =  Criteria.where("platform").regex(pattern);
        list.add(c6);
    }
    //运营平台
    if(StringUtils.isNotEmpty(orderQuery.getPlatform())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getPlatform()) + ".*");
        Criteria c6 =  Criteria.where("platform").regex(pattern);
        list.add(c6);
    }
    if(StringUtils.isNotEmpty(orderQuery.getArea())){
        Pattern pattern = Pattern.compile(".*?" + RgexUtils.escapeExprSpecialWord(orderQuery.getArea()) + ".*");
        Criteria c6 =  Criteria.where("area").regex(pattern);
        list.add(c6);
    }


    orderQuery.setStartTime("2018-12-26");
    orderQuery.setEndTime("2018-12-28");
    orderQuery.setOrderNo("233");

    //订单时间
    if(StringUtils.isNotEmpty(orderQuery.getStartTime())&&StringUtils.isNotEmpty(orderQuery.getEndTime())){
        Criteria c3 =  Criteria.where("time").gte(orderQuery.getStartTime());
        Criteria c4 =  Criteria.where("time").lte(orderQuery.getEndTime());
        list.add(c3);
        list.add(c4);
    }


    PageInfo pageInfo =  orderDao.paginationQuery(mongoTemplate,p,orders,list,clazz);
    return  pageInfo ;
}

步骤二
/**
* 分页查询
* @param mongoTemplate
* @param page
* @param orders
* @param list1
* @param clazz
* @return
*/
@Override
public PageInfo paginationQuery(MongoOperations mongoTemplate, Page page, List orders, List list1, Class clazz){

    SpringbootPageable pageable = new SpringbootPageable();
    PageModel pm=new PageModel();
    if(orders.size()>0){
        Sort sort = new Sort(orders);
        pm.setSort(sort);
    }
    int pageNum = page.getCurrentPage();
    int pageSize = page.getPageSize();
    // 开始页
    pm.setPagenumber(pageNum);
    // 每页条数
    pm.setPagesize(pageSize);
    // 排序
    pageable.setPage(pm);
    Criteria criteria= new Criteria();
    Criteria[] a = new Criteria[list1.size()];
    criteria.andOperator(list1.toArray(a));

    Query query = Query.query(criteria);
    // 查询出一共的条数
    Long count = mongoTemplate.count(query, clazz);

// PageHelper.startPage(pageNum, pageSize);

    // 查询
    List list = mongoTemplate.find(query.with(pageable), clazz);
    // 将集合与分页结果封装

// PageImpl pagelist = new PageImpl(list, pageable, count);
PageInfo p = new PageInfo(list);
p.setPageNum(pageNum);
p.setPageSize(pageSize);
p.setTotal(count);
p.setSize(list.size());
int pages = 0;
if(count%pageSize>0){
pages = (int) (count/pageSize +1);
}
if(count%pageSize0){
pages = (int)(count/pageSize);
}
p.setPages(pages);
if(list.size()>0){
p.setStartRow(1);
}else {
p.setStartRow(0);
}
if(pageNum
1){
p.setIsFirstPage(true);
}else {
p.setIsFirstPage(false);
}
if(pageNumpages){
p.setIsLastPage(true);
}
if(pageNum p.setNextPage(pageNum+1);
p.setHasNextPage(true);
}
if(pageNum
pages){
p.setNextPage(pages);
p.setHasNextPage(false);
}
p.setNavigateFirstPage(pageNum);
p.setNavigateLastPage(pages);
p.setEndRow(list.size());
System.out.println("pageNum = " + pageNum);

   return p;

}

你可能感兴趣的:(mongdb,java)