一.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页面分析