概述
本博文将分析CMS内容管理系统的功能实现,同时借助广告位的展示来介绍解决ajax跨域请求问题的方案二:httpClient。
一、CMS内容管理系统
在后台管理内容及内容分类的系统就叫做cms系统。
1.表设计管理
思路:
1、 分析每个模块的共性:
a)链接 b) 图片 c) 标题 d) 子标题 e) 有链接的提示
2、 使用两张表来管理
a) 内容分类表,管理内容的大分类
b) 内容表,存储每个分类下的明细信息内容。
内容分类表:
内容表:
需要把内容进行分类,分类应该是一个树形结构。
2.内容分类管理
2.1初始化列表
2.1.1需求分析
初始化树形视图的url:/content/category/list
参数是id,当前节点id属性,应该根据此id查询子节点列表。
返回值:包含id、text、state三个属性的json数据列表
2.1.2Service层
功能:接收parentid。根据parentid查询节点列表,返回返回一个EasyUI异步Tree要求的节点列表。每个节点包含三个属性id、text、state三个属性。可以使用EUTreeNode。
参数:id 返回值:List< EUTreeNode >
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
@Autowired
private TbContentCategoryMapper contentCategoryMapper;
@Override
public List<EUITreeNode> getCategoryList(long parentId) {
TbContentCategoryExample example = new TbContentCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
List<EUITreeNode> resultList = new ArrayList<>();
for (TbContentCategory tbContentCategory : list) {
EUITreeNode node = new EUITreeNode();
node.setId(tbContentCategory.getId());
node.setText(tbContentCategory.getName());
node.setState(tbContentCategory.getIsParent()?"closed":"open");
resultList.add(node);
}
return resultList;
}
}
2.1.3Controller层
@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
@Autowired
private ContentCategoryService contentCategoryService;
@RequestMapping("/list")
@ResponseBody
public List<EUITreeNode> getContentCatList(@RequestParam(value="id",defaultValue="0")Long parentId){
List<EUITreeNode> list = contentCategoryService.getCategoryList(parentId);
return list;
}
}
2.2新增节点
2.2.1Dao层
需要返回主键信息:需要修改mapper文件,返回主键信息。
2.2.2Service层
接收两个参数parentId父节点id、name:当前节点的名称。向tb_content_category表中添加一条记录。返回TaoTaoResult包含记录的pojo对象。还需要判断添加的结点的父节点之前是否是父节点,如果不是,则需要改is_parent.
@Override
public TaotaoResult insertContentCategory(long parentId, String name) {
//创建一个pojo
TbContentCategory contentCategory = new TbContentCategory();
contentCategory.setName(name);
contentCategory.setParentId(parentId);
//'状态。可选值:1(正常),2(删除)',
contentCategory.setStatus(1);
contentCategory.setIsParent(false);
contentCategory.setSortOrder(1);
contentCategory.setCreated(new Date());
contentCategory.setUpdated(new Date());
//添加记录
contentCategoryMapper.insert(contentCategory);
//查看父节点的isParent列是否为true,如果不是true改成true
TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(parentId);
if(!parentCat.getIsParent()) {
parentCat.setIsParent(true);
//更新父节点
contentCategoryMapper.updateByPrimaryKey(parentCat);
}
return TaotaoResult.ok(contentCategory);
}
2.2.3Controller层
@RequestMapping("/create")
@ResponseBody
public TaotaoResult createContentCategory(Long parentId, String name) {
TaotaoResult result = contentCategoryService.insertContentCategory(parentId, name);
return result;
}
2.3删除节点
接收parentid、id两个参数。删除id对应的记录。需要判断parentid对应的记录下是否有子节点。如果没有子节点。需要把parentid对应的记录的isparent改成false。
注意:删除直接是物理删除。
service层:
@Override
public TaotaoResult deleteContentCategory(long parentId, long id) {
contentCategoryMapper.deleteByPrimaryKey(id);
TbContentCategoryExample example =