Java的新项目学成在线笔记-day3(一)

1 自定义条件查询 1.1 需求分析 
在页面输入查询条件,查询符合条件的页面信息。
查询条件如下:
站点Id:精确匹配
模板Id:精确匹配
页面别名:模糊匹配
...
1.2 服务端 
1.2.1 Dao 
使用 CmsPageRepository中的findAll(Example<S> var1, Pageable var2)方法实现,无需定义。 
下边测试findAll方法实现自定义条件查询:

//自定义条件查询测试    
      @Test   
  public void testFindAll() {   
     //条件匹配器 
        ExampleMatcher exampleMatcher = ExampleMatcher.matching(); 
exampleMatcher = exampleMatcher.withMatcher("pageAliase",
 ExampleMatcher.GenericPropertyMatchers.contains());     
     //页面别名模糊查询,需要自定义字符串的匹配器实现模糊查询     
    //ExampleMatcher.GenericPropertyMatchers.contains() 包含
 //ExampleMatcher.GenericPropertyMatchers.startsWith()//开头匹配 
              //条件值      
   CmsPage cmsPage = new CmsPage();     
    //站点ID  
       cmsPage.setSiteId("5a751fab6abb5044e0d19ea1");    
     //模板ID       
  cmsPage.setTemplateId("5a962c16b00ffc514038fafd"); //     
  cmsPage.setPageAliase("分类导航");     
    //创建条件实例
        Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);

Pageable pageable = new PageRequest(0, 10);     
   Page<CmsPage> all = cmsPageRepository.findAll(example, pageable);   
     System.out.println(all); 
   }

1.2.2 Service 
在PageService的findlist方法中增加自定义条件查询代码

/**  
   * 页面列表分页查询    
  * @param page 当前页码    
  * @param size 页面显示个数    
  * @param queryPageRequest 查询条件   
   * @return 页面列表    
  */   
 public QueryResponseResult findList(int page,int size,QueryPageRequest queryPageRequest){ 
       //条件匹配器   
      //页面名称模糊查询,需要自定义字符串的匹配器实现模糊查询  
       ExampleMatcher exampleMatcher = ExampleMatcher.matching()                 .withMatcher("pageAliase", ExampleMatcher.GenericPropertyMatchers.contains());      
   //条件值       
  CmsPage cmsPage = new CmsPage();   
      //站点ID   
      if(StringUtils.isNotEmpty(queryPageRequest.getSiteId())){     
       cmsPage.setSiteId(queryPageRequest.getSiteId()); 
        }   
     //页面别名   
      if(StringUtils.isNotEmpty(queryPageRequest.getPageAliase())){      
      cmsPage.setPageAliase(queryPageRequest.getPageAliase());    
     }       
   //创建条件实例    
     Example<CmsPage> example = Example.of(cmsPage, exampleMatcher);    
     //页码  
       page = page‐1;   
      //分页对象    
     Pageable pageable = new PageRequest(page, size);  
       //分页查询      
   Page<CmsPage> all = cmsPageRepository.findAll(example,pageable);  
       QueryResult<CmsPage> cmsPageQueryResult = new QueryResult<CmsPage>();    
     cmsPageQueryResult.setList(all.getContent());   
      cmsPageQueryResult.setTotal(all.getTotalElements());    
     //返回结果      
   return new QueryResponseResult(CommonCode.SUCCESS,cmsPageQueryResult); 
    }

1.2.3 Controller 
无需修改 1.2.4 测试 
使用SwaggerUI测试

Java的新项目学成在线笔记-day3(一)

猜你喜欢

转载自blog.csdn.net/czbkzmj/article/details/85258602