spring boot mongodb 实现多条件查询并分页

    private MongoTemplate mongoTemplate;
    private Pageable pageable;

    @Autowired
    public ResolveRuleService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public Page<ResolveRule> findAllResolveRules(int size, int page, HttpServletRequest request){
        String assetType = request.getParameter("asset_type");
        String assetCompany = request.getParameter("asset_company");
        String assetModel = request.getParameter("asset_model");
        String enable = request.getParameter("enable");

        Query query = new Query();
        Criteria criteria = new Criteria();

        if (StringUtils.isNotBlank(assetType)){
            criteria.and("asset_type").is(assetType);
        }

        if (StringUtils.isNotBlank(assetCompany)){
            criteria.and("asset_company").is(assetCompany);
        }

        if (StringUtils.isNotBlank(assetModel)){
            criteria.and("asset_model").is(assetModel);
        }

        if (StringUtils.isNotBlank(enable)){
            boolean b = BooleanUtils.toBoolean(enable);
            criteria.and("is_enable").is(b);
        }

        query.addCriteria(criteria);

        Pageable pageable = PageRequest.of(page, size);
        List<ResolveRule> resolveRules = mongoTemplate.find(query.with(pageable), ResolveRule.class);

        return PageableExecutionUtils.getPage(resolveRules, pageable,
                () -> mongoTemplate.count(query, ResolveRule.class));
    }

 

展开阅读全文

没有更多推荐了,返回首页