e3mall商城的归纳总结5之修改商品分类、e3mall—content的搭建
说在前面的话
本节基本上没有用到新的知识点。主要还是对数据库的增删改查以及创建了一个新的内容模块。
新增商品分类
由于easyUI的Tree需要三个字段(Id、state、text),
[{
"id": 1,
"text": "Node 1",
"state": "closed"
},{
"id": 2,
"text": "Node 2",
"state": "closed"
}]
因此做了一个工具类EasyUITreeNode ,方便调用返回对象。
public class EasyUITreeNode implements Serializable{
private long id;
private String text;
private String state;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
当页面加载后自动获取tree目录,此时parent_id为空,因此我们再后台设置一下若parent_id为空,则默认为0.0为一级目录.
@RequestMapping("/content/category/list")
@ResponseBody
public List<EasyUITreeNode> getContentCat(@RequestParam(defaultValue="0",name="id")Long parent_id){
return contentCatService.getContentCategory(parent_id);
}
service层:
@Override
public List<EasyUITreeNode> getContentCategory(Long id) {
TbContentCategoryExample example = new TbContentCategoryExample();
example.createCriteria().andParentIdEqualTo(id);
List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
List<EasyUITreeNode> results = new ArrayList<>();
for (TbContentCategory l : list) {
EasyUITreeNode easyUITreeNode = new EasyUITreeNode();
easyUITreeNode.setId(l.getId());
easyUITreeNode.setState(l.getIsParent()?"closed":"open");
easyUITreeNode.setText(l.getName());
results.add(easyUITreeNode);
}
return results;
}
创建分类url:/content/category/create
逻辑介绍:当用户右击contentCategory的节点时,激发了js效果,则显示添加、重命名、删除三个选项。
当用户点击添加时,contentCategory在选择的节点上创建一个新的叶子节点,并且把该节点的id设置为0,parentId设置为叶子节点的父节点(用户点击的节点),text设置为“新建分类”,然后在整个tree中寻找id为0的节点,然后把该节点设置为可编辑状态
如图:
当用户结束编辑时(onAfterEdit),触发了onAfterEdit事件(此事件可能被两种情况触发1、创建新节点;2、重命名节点)若是第一种情况,则节点的id为0,所以进行判断,若节点的id为0,则向服务器发送创建节点请求,反之发送更改节点请求。
创建节点的请求需要发送parentId和name,若成功,系统需要返回该节点的id和status(用e3result对象返回),然后再把id的值(id=0新创建的节点id都为0)改为新的id的值(服务器返回的值)。
更改节点的请求发送本节点的id和text即可。
代码如图:
service层:
当该节点创建新的节点后,该节点一定是父亲节点(isparent=true),新的节点必须是叶子节点(isparent=false),因此service需要进行两步
//创建内容管理节点
@Override
public E3Result createNodeCat(Long parentId, String name) {
//补充数据
TbContentCategory record = new TbContentCategory();
record.setCreated(new Date());
record.setIsParent(false);//新建的一定是叶子节点
record.setName(name);
record.setParentId(parentId);
record.setSortOrder(1);//默认1排序
record.setStatus(1);//可选值:1(正常),2(删除)',
record.setUpdated(new Date());
//添加数据(设置了select LAST_INSERT_ID())
contentCategoryMapper.insertSelective(record);
//把它的父亲节点变成is_parent
TbContentCategoryExample example = new TbContentCategoryExample();
Criteria createCriteria = example.createCriteria();
createCriteria.andIdEqualTo(parentId);
//创建一个category对象,除了isparent为true,其他都为null
TbContentCategory category = new TbContentCategory();
category.setIsParent(true);
//只修改不为空的数据
//把父节点更改的isparent改为true
contentCategoryMapper.updateByExampleSelective(category, example);
return E3Result.ok(record);
}
controller层:
//创建新的节点
@RequestMapping("/content/category/create")
@ResponseBody
public E3Result createNodeCat(Long parentId,String name){
return contentCatService.createNodeCat(parentId, name);
}
修改商品分类
修改商品逻辑同上,直接贴代码
service层:
@Override
public void updateNodeCat(Long id, String name) {
TbContentCategoryExample example = new TbContentCategoryExample();
example.createCriteria().andIdEqualTo(id);
//创建pojo
TbContentCategory record = new TbContentCategory();
record.setName(name);
contentCategoryMapper.updateByExampleSelective(record, example);
}
controller层:
@RequestMapping("/content/category/update")
@ResponseBody
public void updateNodeCat(Long id,String name){
contentCatService.updateNodeCat(id, name);
}
当用户点击重命名(楼上已说)和删除后的逻辑代码(点击删除向服务器发送删除请求)
删除商品分类
service层:
@Override
public void deleteNodeCat(Long id) {
contentCategoryMapper.deleteByPrimaryKey(id);
}
controller层:
@RequestMapping("/content/category/delete/")
@ResponseBody
public void deleteNodeCat(Long id){
contentCatService.deleteNodeCat(id);
}
创建e3mall-content模块
同第一天略