学成在线day03,cms的CRUD,和统一异常处理

本文档详述了如何在Studio3T中建立数据库索引,定义API接口,并实现CMS的CRUD操作。同时,提供了代码注释和前端页面展示,包括Cms的列表、新增和修改功能。此外,还介绍了服务端的异常处理,包括自定义异常类和跨域中间件配置。
摘要由CSDN通过智能技术生成

需求:
在这里插入图片描述

去Studio3T,建立数据库的相关索引

在这里插入图片描述

先定义Api接口

在这里插入图片描述

//@Api:修饰整个类,描述Controller的作用
//value 描述这个接口,是cms页面管理接口
//description 这个接口所负责的具体工作
@Api(value="cms页面管理接口",description = "cms页面管理接口,提供页面的增、删、改、查")
public interface CmsPageControllerApi {
   
    //@ApiOperation:描述一个类的一个方法或接口,描述这个方法的具体工作内容
    @ApiOperation("分页查询页面列表")
    //@ApiImplicitParams:该方法有多个请求参数
    @ApiImplicitParams({
   
            //page这个参数是指页码,必填,网址路径上获取,数据类型为int
            @ApiImplicitParam(name="page",value = "页 码",required=true,paramType="path",dataType="int"),
            //size这个参数是指每页显示条数,必填,网址路径上获取,数据类型为int
            @ApiImplicitParam(name="size",value = "每页记录数",required=true,paramType="path",dataType="int")
    })
    //*******具体的方法接口*************
    public QueryResponseResult findList(int page, int size,
                                        QueryPageRequest queryPageRequest);

    //*******新增cms页面接口方法*************
    @ApiOperation("新增cms页面")
    @ApiImplicitParam(name="cmsPage",value = "新增对象",required=true,dataType="CmsPage")
    public CmsPageResult add(CmsPage cmsPage);


    //根据页面id查询页面信息
    @ApiOperation("根据页面id查询页面信息")
    public CmsPage findById(String id);
    //修改页面
    @ApiOperation("修改页面")
    public CmsPageResult edit(String id,CmsPage cmsPage);

    //删除页面
    @ApiOperation("删除页面")
    public ResponseResult delete(String id);
}

实现cms全部的功能需求代码。具体代码都有详细解释和备注

###dao层####
在这里插入图片描述

//dao层接口继承mongoDB数据库类。
public interface CmsPageRepository extends MongoRepository<CmsPage,String> {
   
    //用于校验是否已存在相同页面的方法。通过页面名称,路径,站点id,校验
    CmsPage findByPageNameAndPageWebPathAndSiteId(String pageName,String pageWebPath,String siteId);
}

###Service层####
在PageService的findlist方法中增加自定义条件查询代码:
在这里插入图片描述

@Service
public class PageService {
   
    @Autowired
    private CmsPageRepository cmsPageRepository;

    /**
      * 页面列表分页查询
      * @param page 当前页码
      * @param size 页面显示个数
      * @param queryPageRequest 查询条件
      * @return 页面列表
      */
    public QueryResponseResult findList(int page, int size,  QueryPageRequest queryPageRequest) {
   

        //判断传进来的页面,小于第一页,就默认查询第一页
        if (page <= 0){
   
            page = 1;
        }
        //如果传进来的不是小于0,证明页面正确,就-1,因为数据库是从0开始,
        page = page - 1;

        //判断传进来的显示数量,小于10,就默认查询10条
        if (size <= 0){
   
            size = 10;
        }
        //组装查询所需要的页码和数量,的pageable对象
        Pageable pageable = PageRequest.of(page,size);
        //__________分页信息组装好后,开始组装条件查询_____________

        //**开始**提前准备一个空的条件查询对象
        Example<CmsPage> example = null;

        //****先判断,条件查询的对象是否为空,不为空,才开始组装查询条件****
        if (queryPageRequest!=null){
   
            //先准备一个cms分页对象。
            CmsPage cmsPage = new CmsPage();

            //如果站点id,不为空,就组装cms对象
            String siteId = queryPageRequest.getSiteId();
            if (siteId!=null && !siteId.equals("")){
   
                cmsPage.setSiteId(siteId);
            }

            //如果站点模板id,不为空,就组装cms对象
            String TemplateId = queryPageRequest.getTemplateId();
            if (TemplateId!=null && !TemplateId.equals("")){
   
                cmsPage.setTemplateId(TemplateId);
            }

            //如果别名,不为空,就组装cms对象
            String PageAliase = queryPageRequest.getPageAliase();
            if (PageAliase!=null && !PageAliase.equals("")){
   
                cmsPage.setPageAliase(PageAliase);
            }

            //别名属于模糊查询,所以需要,设置模糊条件查询匹配器
                //ExampleMatcher.GenericPropertyMatchers.contains() 包含关键字
                //ExampleMatcher.GenericPropertyMatchers.startsWith() 前缀匹配
            ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                    //链式点出,通过关键字匹配,数据库中的PageAliase列
                    .withMatcher("pageAliase",ExampleMatcher.GenericPropertyMatchers.contains());
            //把组装好的cms条件查询对象和模糊条件匹配器,
                // 添加进条件查询的静态方法中。得到一个条件查询对象
            example = Example.of(cmsPage,exampleMatcher);
        }

//********分页信息组装好后,开始组装条件查询结束********



        //执行查询,参数1,条件查询的对象,参数2,分页参数值对象
        Page<CmsPage> cmsPagePageList = cmsPageRepository.findAll(example,pageable);
        System.err.println(cmsPagePageList.getContent());
        //封装分页数据集合和总记录数的对象
        QueryResult queryResult = new QueryResult();
        //得到分页数据集合
        queryResult.setList(cmsPagePageList.getContent());
        // 得到总记录数
        queryResult.setTotal(cmsPagePageList.getTotalElements());
        //封装返回到页面的固定格式对象
        QueryResponseResult queryResponseResult =
                new QueryResponseResult(CommonCode.SUCCESS,queryResult);
        return queryResponseResult;
    }

    //_________新增页面方法,
    public CmsPageResult add(CmsPage cmsPage){
   

        if (cmsPage == null) {
   
            //证明进来的参数没有,也就是参数异常
            ExceptionCast.cast(CommonCode.INVALID_PARAM);
        }


            //进来的对象,不为空,就执行校验
            CmsPage page = cmsPageRepository.findByPageNameAndPageWebPathAndSiteId
                    (cmsPage.getPageName(), cmsPage.getPageWebPath(), cmsPage.getSiteId());
            if (page == null) {
   
                //如果校验没有这个页面,就执行添加页面的操作
                //添加页面之前,先把页面id,清除,让MongoDB数据库自己生成主键
                cmsPage.setPageId(null);
                //执行添加页面的操作
                CmsPage cmsPage1 = cmsPageRepository.save(cmsPage);
                //返回resultful规范数据
                return new CmsPageResult(CommonCode.SUCCESS,cmsPage1);
            }
            //如果校验到了,就直接不让新增
            ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);
            //返回一个null,防止语法报错,前面的异常会拦截,下面是不可达代码
            return null;
    }

    //修改操作,包括,数据单查回显,数据修改2个方法

    //单查回显
    public CmsPage getById(String id){
   
        //单查取得optional对象
        Optional<CmsPage> byId = cmsPageRepository.findById(id);
        //判断optional对象不为空就取cms页面,进行返回
        if (byId.isPresent()) {
   
            CmsPage cmsPage = byId.get();
            return cmsPage;
        }
        return null;
    }


    //数据修改
    public CmsPageResult edit(String id,CmsPage cmsPage){
   
        //先查询通过id要修改的对象
        CmsPage one = getById(id);
        //手动设置其中的属性,原因在于,有些列不允许修改
        if(one!=null){
   
            //准备更新数据
            //设置要修改的数据
            //更新模板id
            one.setTemplateId(cmsPage.getTemplateId());
            //更新所属站点
            one.setSiteId(cmsPage.getSiteId());
            //更新页面别名
            one.setPageAliase(cmsPage.getPageAliase());
            //更新页面名称
            one.setPageName(cmsPage.getPageName());
            //更新访问路径
            one.setPageWebPath(cmsPage.getPageWebPath());
            //更新物理路径
            one.setPagePhysicalPath(cmsPage.getPagePhysicalPath());
            //提交修改
            cmsPageRepository.save(one);
            return new CmsPageResult(CommonCode.SUCCESS,one);
        }
        //如果没有找到cms对象,就返回失败
        return new CmsPageResult(CommonCode.FAIL,null);
    }

    //删除cms页面
    public ResponseResult <
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值