5Cms系统实现
5.1内容分类管理
5.1.1展示内容分类
功能分析
请求的url:/content/category/list
请求的参数:id,当前节点的id。第一次请求是没有参数,需要给默认值“0”
响应数据:List<EasyUITreeNode>(@ResponseBody)
Json数据。
[{id:1,text:节点名称,state:open(closed)},
{id:2,text:节点名称2,state:open(closed)},
{id:3,text:节点名称3,state:open(closed)}]
业务逻辑:
- 取查询参数id,parentId
- 根据parentId查询tb_content_category,查询子节点列表。
- 得到List<TbContentCategory>
- 把列表转换成List<EasyUITreeNode>
Dao层
使用逆向工程
Service
参数:long parentId
返回值:List<EasyUITreeNode>
@Service public class ContentCategoryServiceImpl implements ContentCategoryService {
@Autowired private TbContentCategoryMapper contentCategoryMapper;
@Override public List<EasyUITreeNode> getContentCategoryList(long parentId) { // 1、取查询参数id,parentId // 2、根据parentId查询tb_content_category,查询子节点列表。 TbContentCategoryExample example = new TbContentCategoryExample(); //设置查询条件 Criteria criteria = example.createCriteria(); criteria.andParentIdEqualTo(parentId); //执行查询 // 3、得到List<TbContentCategory> List<TbContentCategory> list = contentCategoryMapper.selectByExample(example); // 4、把列表转换成List<EasyUITreeNode>ub List<EasyUITreeNode> resultList = new ArrayList<>(); for (TbContentCategory tbContentCategory : list) { EasyUITreeNode node = new EasyUITreeNode(); node.setId(tbContentCategory.getId()); node.setText(tbContentCategory.getName()); node.setState(tbContentCategory.getIsParent()?"closed":"open"); //添加到列表 resultList.add(node); } return resultList; }
} |
发布服务
表现层(在哪个表现层调用就在哪里引用dubbo服务)
E3-manager-web
依赖e3-content-interface工程模块
Springmvc.xml中添加服务的引用:
Controller
@Controller @RequestMapping("/content/category") public class ContentCategoryController {
@Autowired private ContentCategoryService contentCategoryService;
@RequestMapping("/list") @ResponseBody public List<EasyUITreeNode> getContentCatList( @RequestParam(value="id", defaultValue="0") Long parentId) {
List<EasyUITreeNode> list = contentCategoryService.getContentCategoryList(parentId); return list; } } |
5.1.2 新增节点
功能分析
请求的url:/content/category/create
请求的参数:
Long parentId
String name
响应的结果:
json数据,E3Result,其中包含一个对象,对象有id属性,新生产的内容分类id
业务逻辑:
- 接收两个参数:parentId、name
- 向tb_content_category表中插入数据。
- 创建一个TbContentCategory对象
- 补全TbContentCategory对象的属性
- 向tb_content_category表中插入数据
- 判断父节点的isparent是否为true,不是true需要改为true。
- 需要主键返回。
- 返回E3Result,其中包装TbContentCategory对象
Dao层
可以使用逆向工程。
需要添加主键返回:
Service层
参数:parentId、name
返回值:返回E3Result,其中包装TbContentCategory对象
@Override public E3Result addContentCategory(long parentId, String name) { // 1、接收两个参数:parentId、name // 2、向tb_content_category表中插入数据。 // a)创建一个TbContentCategory对象 TbContentCategory tbContentCategory = new TbContentCategory(); // b)补全TbContentCategory对象的属性 tbContentCategory.setIsParent(false); tbContentCategory.setName(name); tbContentCategory.setParentId(parentId); //排列序号,表示同级类目的展现次序,如数值相等则按名称次序排列。取值范围:大于零的整数 tbContentCategory.setSortOrder(1); //状态。可选值:1(正常),2(删除) tbContentCategory.setStatus(1); tbContentCategory.setCreated(new Date()); tbContentCategory.setUpdated(new Date()); // c)向tb_content_category表中插入数据 contentCategoryMapper.insert(tbContentCategory); // 3、判断父节点的isparent是否为true,不是true需要改为true。 TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId); if (!parentNode.getIsParent()) { parentNode.setIsParent(true); //更新父节点 contentCategoryMapper.updateByPrimaryKey(parentNode); } // 4、需要主键返回。 // 5、返回E3Result,其中包装TbContentCategory对象 return E3Result.ok(tbContentCategory); } |
发布服务。
表现层
请求的url:/content/category/create
请求的参数:
Long parentId
String name
响应的结果:
json数据,E3Result
@RequestMapping("/create") @ResponseBody public E3Result createCategory(Long parentId, String name) { E3Result result = contentCategoryService.addContentCategory(parentId, name); return result; } |
5.1.3内容分类重命名、删除
重命名
请求的url:/content/category/update
参数:id,当前节点id。name,重命名后的名称。
业务逻辑:根据id更新记录。
返回值:返回E3Result.ok()
作业。
删除节点
请求的url:/content/category/delete/
参数:id,当前节点的id。
响应的数据:json。E3Result。
业务逻辑:
- 根据id删除记录。
- 判断父节点下是否还有子节点,如果没有需要把父节点的isparent改为false
- 如果删除的是父节点,子节点要级联删除。
两种解决方案:
- 如果判断是父节点不允许删除。
- 递归删除。
作业。