内容服务系统——内容分类管理
展示内容分类
最终目标:
-
请求的 url :
/content/category/list
-
请求的参数:id,当前节点的 id 。第一次请求是没有参数,需要给默认值“0”
-
响应数据:List(@ResponseBody)
Json数据。
[{id:1,text:节点名称,state:open(closed)},
{id:2,text:节点名称2,state:open(closed)},
{id:3,text:节点名称3,state:open(closed)}] -
业务逻辑:
1、取查询参数id,parentId
2、根据parentId查询tb_content_category,查询子节点列表。
3、得到List
4、把列表转换成List
Dao层
使用逆向工程
Service
参数:long parentId
返回值:List
package cn.ynx.e3mall.content.service;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import java.util.List;
public interface ContentCategoryService {
List<EasyUITreeNode> getContentCategoryList(long parentId);
}
package cn.ynx.e3mall.content.service.impl;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.content.service.ContentCategoryService;
import cn.ynx.e3mall.mapper.TbContentCategoryMapper;
import cn.ynx.e3mall.pojo.TbContentCategory;
import cn.ynx.e3mall.pojo.TbContentCategoryExample;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
@Resource
private TbContentCategoryMapper tbContentCategoryMapper;
@Override
public List<EasyUITreeNode> getContentCategoryList(long parentId) {
// 1、取查询参数id,parentId
// 2、根据parentId查询tb_content_category,查询子节点列表。
TbContentCategoryExample example = new TbContentCategoryExample();
//设置查询条件
TbContentCategoryExample.Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
//执行查询
// 3、得到List<TbContentCategory>
List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
// 4、把列表转换成List<EasyUITreeNode>
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;
}
}
发布服务:applicationContrxt-service.xml
<dubbo:service interface="cn.ynx.e3mall.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" timeout="300000" />
表现层
e3-manager-web
依赖 e3-content-interface 模块
<!-- 依赖 e3-content-interface 模块 -->
<dependency>
<groupId>cn.ynx.e3mall</groupId>
<artifactId>e3-content-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
springmvc.xml 中添加服务的引用:
<dubbo:reference interface="cn.ynx.e3mall.content.service.ContentCategoryService" id="contentCategoryService" />
Controller
package cn.ynx.e3mall.controller;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.content.service.ContentCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {
@Resource
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;
}
}
启动manager和content服务,必须两个都启动,不然web启动会报错。
测试
新增节点
功能分析:
Easy UI 的 tree 提供了添加、重命名、删除的功能,会异步发送 ajax 请求。
-
请求的 url:
/content/category/create
-
请求的参数:
Long parentId
String name -
响应的结果:
json数据,E3Result,其中包含一个对象,对象有id属性,新生产的内容分类id -
业务逻辑:
- 1、接收两个参数:parentId、name
- 2、向tb_content_category表中插入数据。
a) 创建一个TbContentCategory对象
b) 补全TbContentCategory对象的属性
c) 向tb_content_category表中插入数据 - 3、判断父节点的 isparent 是否为true,不是true需要改为true。
- 4、需要主键返回。
- 5、返回 E3Result ,其中包装 TbContentCategory 对象
Dao层
可以使用逆向工程。
需要添加主键返回: 修改逆向工程的代码
Service层
参数:parentId、name
返回值:返回 E3Result ,其中包装 TbContentCategory 对象
package cn.ynx.e3mall.content.service;
import cn.ynx.e3mall.common.pojo.EasyUITreeNode;
import cn.ynx.e3mall.common.utils.E3Result;
import java.util.List;
public interface ContentCategoryService {
List<EasyUITreeNode> getContentCategoryList(long parentId);
E3Result addContentCategory(long parentId, String name);
}
@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表中插入数据
tbContentCategoryMapper.insert(tbContentCategory);
// 3、判断父节点的isparent是否为true,不是true需要改为true。
TbContentCategory parentNode = tbContentCategoryMapper.selectByPrimaryKey(parentId);
if (!parentNode.getIsParent()) {
parentNode.setIsParent(true);
//更新父节点
tbContentCategoryMapper.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 createContentCategory(Long parentId, String name){
E3Result result = contentCategoryService.addContentCategory(parentId, name);
return result;
}
测试
内容分类节点重命名
- 请求的url:/content/category/update
- 参数:
id,当前节点id。
name,重命名后的名称。 - 业务逻辑:根据id更新记录。
- 返回值:返回E3Result.ok()
内容分类节点删除
- 请求的url:/content/category/delete/
- 参数:id,当前节点的id。
- 响应的数据:json。E3Result。
- 业务逻辑:
1、根据id删除记录。
2、判断父节点下是否还有子节点,如果没有需要把父节点的isparent改为false
3、如果删除的是父节点,子节点要级联删除。
两种解决方案:
1)如果判断是父节点不允许删除。
2)递归删除。