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

2 新增页面 2.1 新增页面接口定义
1、定义响应模型

@Data 
public class CmsPageResult extends ResponseResult { 
   CmsPage cmsPage;  
   public CmsPageResult(ResultCode resultCode,CmsPage cmsPage) {    
    super(resultCode);       
  this.cmsPage = cmsPage;   
  }
}

2、定义添加Api 在api工程中添加接口:

@ApiOperation("添加页面")
public CmsPageResult add(CmsPage cmsPage);

2.2 新增页面服务端开发
2.2.1 页面唯一索引
在cms_page集中上创建页面名称、站点Id、页面webpath为唯一索引 2.2.2 Dao
1、添加根据页面名称、站点Id、页面webpath查询页面方法,此方法用于校验页面是否存在

public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
    //根据页面名称、站点id、页面访问路径查询   
  CmsPage findByPageNameAndSiteIdAndPageWebPath(String pageName,String siteId,String  pageWebPath);  
   。。

2、使用 CmsPageRepository提供的save方法 。 2.2.3 Service

//添加页面   
  public CmsPageResult add(CmsPage cmsPage){ //校验页面是否存在,
根据页面名称、站点Id、页面webpath查询       
           CmsPage cmsPage1 =
 cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(),
 cmsPage.getSiteId(), cmsPage.getPageWebPath());      
    if(cmsPage1==null){        
    cmsPage.setPageId(null);//添加页面主键由spring data 自动生成      
       cmsPageRepository.save(cmsPage);       
      //返回结果     
        CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage);      
       return cmsPageResult;       
  }   
     return new CmsPageResult(CommonCode.FAIL,null); 
    }

2.2.4 Controller

//添加页面  
  @Override 
   @PostMapping("/add") 
  public CmsPageResult add(@RequestBody CmsPage cmsPage) { 
      return pageService.add(cmsPage);  
  }

猜你喜欢

转载自blog.51cto.com/13517854/2336079