百战商城(五)-CMS系统

本文深入解析了CMS系统在电商后台的应用,涵盖了内容类目管理、内容信息管理等核心功能,详细介绍了类目增删改查及内容信息的列表查询与新增操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.CMS

1.CMS, Content Management System, 内容管理系统. 用于对前台系统中的内容进行管理, 例如广告信息的管理, 公告管理, 楼层管理, 活动信息管理…
2.实际开发中, CMS应该是一个独立的系统. 百战商城中, CMS被嵌入到后台管理系统中.内容管理局限于大广告信息管理.
3.涉及到的数据库表格
a)tb_content_category, 内容类目表.
b)tb_content, 内容表

二.内容类目管理

1.内容类目列表查询
1.1页面分析
content-category.jsp
在这里插入图片描述
在这里插入图片描述
1.2控制器

@RestController
@RequestMapping("/content/category")
public class ContentCatController {
    @Autowired
    private ContentCatService contentCatService;

    /**
     * 根据父id查询内容类目列表
     *
     * @param pid
     * @return
     */
    @GetMapping("/list")
    public List<EasyTreeNode> contentCatList(
            @RequestParam(value = "id", defaultValue = "0") long pid) {
        return contentCatService.contentCatList(pid);
    }
}

1.3后台系统服务代码

public interface ContentCatService {
    /**
     * 内容类目列表查询
     *
     * @param pid
     * @return
     */
    List<EasyTreeNode> contentCatList(long pid);
}
@Service
public class ContentCatServiceImpl implements ContentCatService {
    @Autowired
    private RpcContentCatService rpcContentCatService;

    @Override
    public List<EasyTreeNode> contentCatList(long pid) {
        // 准备要返回的结果对象
        List<EasyTreeNode> list = new ArrayList<>();
        try {
            // 远程调用获取结果
            RpcResult<List<ContentCategory>> result =
                    rpcContentCatService.contentCatListByPid(pid);
            // 判断结果状态, 进行类型转换
            if(result.getStatus() == 200) {
                // 获取数据
                List<ContentCategory> data = result.getData();
                for (ContentCategory category : data) {
                    EasyTreeNode node = new EasyTreeNode();
                    node.setId(category.getId());
                    node.setText(category.getName());
                    node.setState(category.getIsParent() ? "closed" : "open");
                    list.add(node);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回结果
        return list;
    }
}

1.4RPC系统服务代码

public interface RpcContentCatService {
    /**
     * 根据父类目id查询类目列表信息
     *
     * @param pid
     * @return
     */
    RpcResult<List<ContentCategory>> contentCatListByPid(long pid);
}
@Service
public class RpcContentCatServiceImpl implements RpcContentCatService {
    @Autowired
    private ContentCategoryMapper contentCategoryMapper;

    @Override
    public RpcResult<List<ContentCategory>> contentCatListByPid(long pid) {
        // 创建条件
        ContentCategoryExample example = new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        // 调用mapper进行查询
        List<ContentCategory> list = contentCategoryMapper.selectByExample(example);
        // 返回对象
        return RpcResult.ok(list);
    }
}

1.5dubbo配置(略)
2.添加内容类目
2.1页面分析
content-category.jsp
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.2控制器

/**
 * 新增内容类目
 *
 * @param contentCategory
 * @return
 */
@PostMapping("/create")
public RpcResult<ContentCategory> create(ContentCategory contentCategory) {
    return contentCatService.create(contentCategory);
}

2.3后台系统服务代码

/**
 * 新增内容类目
 *
 * @param contentCategory
 * @return
 */
RpcResult<ContentCategory> create(ContentCategory contentCategory);
@Override
public RpcResult<ContentCategory> create(ContentCategory contentCategory) {
    // 当前时间
    Date date = new Date();
    // 完善对象
    contentCategory.setCreated(date);
    contentCategory.setUpdated(date);
    contentCategory.setSortOrder(1);
    contentCategory.setStatus(1);
    contentCategory.setIsParent(false);
    try {
        // 远程调用进行新增
        return rpcContentCatService.createContentCat(contentCategory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 返回结果
    return RpcResult.build(500, null, "新增失败!");
}

2.4RPC系统服务代码

/**
 * 新增内容类目
 *
 * @param contentCategory
 * @return
 */
RpcResult<ContentCategory> createContentCat(ContentCategory contentCategory);
@Override
@Transactional
public RpcResult<ContentCategory> createContentCat(ContentCategory contentCategory) {
    // 查询
    ContentCategory category = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
    // 判断
    if(!category.getIsParent()) {
        ContentCategory parent = new ContentCategory();
        parent.setId(contentCategory.getParentId());
        parent.setIsParent(true);
        // 更新
        int rows = contentCategoryMapper.updateByPrimaryKeySelective(parent);
        // 判断
        if(rows != 1) {
            throw new RuntimeException("父类目状态修改失败!");
        }
    }
    // 新增类目节点
    int rows = contentCategoryMapper.insert(contentCategory);
    if(rows != 1) {
        throw new RuntimeException("新增失败!");
    }
    return RpcResult.ok(contentCategory);
}

3.重命名内容类目
3.1页面分析
在这里插入图片描述
在这里插入图片描述
3.2控制器

/**
 * 重命名
 *
 * @param contentCategory
 * @return
 */
@PostMapping("/update")
public RpcResult<?> update(ContentCategory contentCategory) {
    return contentCatService.update(contentCategory);
}

3.3后台系统服务代码

/**
 * 重命名
 *
 * @param contentCategory
 * @return
 */
RpcResult<?> update(ContentCategory contentCategory);
@Override
public RpcResult<?> update(ContentCategory contentCategory) {
    // 完善对象
    contentCategory.setUpdated(new Date());
    try {
        // 远程调用返回结果
        return rpcContentCatService.update(contentCategory);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return RpcResult.error();
}

3.4RPC系统服务代码

/**
 * 重命名
 *
 * @param contentCategory
 * @return
 */
RpcResult<?> update(ContentCategory contentCategory);
@Override
@Transactional
public RpcResult<?> update(ContentCategory contentCategory) {
    int rows = contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
    if(rows == 1) {
        return RpcResult.ok();
    }
    throw new RuntimeException("重命名失败!");
}

4.删除类目
4.1页面分析
在这里插入图片描述
4.2控制器

/**
 * 根据类目id删除类目数据
 *
 * @param id
 * @return
 */
@PostMapping("/delete")
public RpcResult<?> delete(long id) {
    return contentCatService.delete(id);
}

4.3后台系统服务代码

/**
 * 删除类目
 *
 * @param id
 * @return
 */
RpcResult<?> delete(long id);
@Override
public RpcResult<?> delete(long id) {
    try {
        return rpcContentCatService.deleteById(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return RpcResult.error();
}

4.4RPC系统服务代码

/**
 * 根据类目id删除类目数据
 *
 * @param id
 * @return
 */
RpcResult<?> deleteById(long id);
@Override
@Transactional
public RpcResult<?> deleteById(long id) {
    // 查询当前要删除的类目
    ContentCategory category = contentCategoryMapper.selectByPrimaryKey(id);
    // 判断是否是顶级类目
    if(category.getParentId().equals(0L)) {
        // 顶级类目
        return RpcResult.build(500, null, "顶级类目无法删除!");
    }
    // 判断是否是父类目
    if(category.getIsParent()) {
        // 递归删除其子类目
        delByPid(category.getId());
    }
    // 创建条件对象
    ContentCategoryExample example = new ContentCategoryExample();
    ContentCategoryExample.Criteria criteria = example.createCriteria();
    criteria.andParentIdEqualTo(category.getParentId());
    // 查询当前类目的父类目下的所有子类目
    List<ContentCategory> list = contentCategoryMapper.selectByExample(example);
    // 判断父类目是否需要更改节点状态
    if(list.size() == 1) {
        // 修改父类目的节点状态
        ContentCategory parent = new ContentCategory();
        parent.setId(category.getParentId());
        parent.setIsParent(false);
        // 执行修改操作
        int rows = contentCategoryMapper.updateByPrimaryKeySelective(parent);
        // 判断
        if(rows != 1) {
            throw new RuntimeException("状态修改失败!");
        }
    }
    // 删除当前类目
    int rows = contentCategoryMapper.deleteByPrimaryKey(id);
    // 判断状态
    if(rows != 1) {
        throw new RuntimeException("类目删除失败!");
    }
    // 返回结果
    return RpcResult.ok();
}

/**
 * 根据父类目id递归删除父类目的所有子类目
 *
 * @param pid
 */
private void delByPid(Long pid) {
    // 创建条件对象
    ContentCategoryExample example = new ContentCategoryExample();
    ContentCategoryExample.Criteria criteria = example.createCriteria();
    criteria.andParentIdEqualTo(pid);
    // 查询pid下的所有子类目
    List<ContentCategory> list = contentCategoryMapper.selectByExample(example);
    // 遍历进行删除
    for (ContentCategory category : list) {
        // 判断是否父类目
        if(category.getIsParent()) {
            // 父类目, 递归删除
            delByPid(category.getId());
        }
        // 删除当前类目
        int rows = contentCategoryMapper.deleteByPrimaryKey(category.getId());
        // 判断结果
        if(rows != 1) {
            throw new RuntimeException("递归删除失败!");
        }
    }
}

三.内容管理
1.内容信息的列表查询
1.1页面分析
content.jsp
在这里插入图片描述
1.2控制器

/**
 * 根据类目id分页查询内容信息
 *
 * @param categoryId
 * @param page
 * @param rows
 * @return
 */
@GetMapping("/list")
public EasyGrid<Content> contentList(
        long categoryId,
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int rows) {
    return contentService.contentListByCategoryId(categoryId, page, rows);
}

1.3后台系统服务代码

/**
 * 根据内容分类查询内容信息
 *
 * @param categoryId
 * @param page
 * @param rows
 * @return
 */
EasyGrid<Content> contentListByCategoryId(long categoryId, int page, int rows);
@Override
public EasyGrid<Content> contentListByCategoryId(long categoryId, int page, int rows) {
    // 准备返回对象
    EasyGrid<Content> grid = new EasyGrid<>();
    try {
        // 远程调用
        RpcResult<PageInfo<Content>> result =
                rpcContentService.contentListByCid(categoryId, page, rows);
        // 判断结果状态
        if(result.getStatus() == 200) {
            // 获取数据
            PageInfo<Content> data = result.getData();
            // 赋值
            grid.setRows(data.getList());
            grid.setTotal(data.getTotal());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // 返回结果
    return grid;
}

1.4RPC系统服务代码

/**
 * 根据类目编号查询内容列表
 *
 * @param categoryId
 * @param page
 * @param rows
 * @return
 */
RpcResult<PageInfo<Content>> contentListByCid(long categoryId, int page, int rows);
@Override
public RpcResult<PageInfo<Content>> contentListByCid(long categoryId, int page, int rows) {
    // 开启分页查询
    PageHelper.startPage(page, rows);
    // 创建条件
    ContentExample example = new ContentExample();
    ContentExample.Criteria criteria = example.createCriteria();
    criteria.andCategoryIdEqualTo(categoryId);
    // 查询
    List<Content> list = contentMapper.selectByExampleWithBLOBs(example);
    // 封装
    PageInfo<Content> pageInfo = new PageInfo<>(list);
    // 返回结果
    return RpcResult.ok(pageInfo);
}

1.5 配置dubbo(略)
2.内容新增
2.1页面分析
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值