根据mongoDB进行条件查询

根据mongoDB进行条件查询

一言不合上代码

 //测试条件查询
    @Test
    public void findAllTest(){
        //分页查询
        int page = 0;
        int size = 1;
        PageRequest pageable = PageRequest.of(page, size);

        //条件匹配,构建条件
        CmsPage cmsPage = new CmsPage();
        cmsPage.setPageName("index.html");
        //构建匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        //构建匹配器的匹配器条件,(想要模糊查询的字段,匹配规则)
        //ExampleMatcher.GenericPropertyMatchers.contains():代表包含,即模糊匹配
        //Exa
        exampleMatcher = exampleMatcher.withMatcher("pageName",ExampleMatcher.GenericPropertyMatchers.contains());
        //ExampleMatcher.GenericPropertyMatchers.endsWith():代表结尾匹配
        Example<CmsPage> example = Example.of(cmsPage,exampleMatcher);
        //根据分页以及条件进行查询
        Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);
        int i = all.getTotalPages();
        System.out.println(i);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43794897/article/details/84989487