一、分析
1.查看jsp代码,其参数为id(只需id参数即可删除),url为/content/category/delete
else if(item.name === "delete"){
$.messager.confirm('确认','确定删除名为 '+node.text+' 的分类吗?',function(r){
if(r){
$.post("/content/category/delete/",{parentId:node.parentId,id:node.id},function(){
tree.tree("remove",node.target);
});
}
});
}
2.Dao层,因为是单表查询,直接使用逆向工程生成的pojo
二、.Service层
1.定义一个接口
public interface ContentCategoryService {
TaotaoResult deleteContentCategory(long id);// 删除
}
2.实现
@Override
public TaotaoResult deleteContentCategory(long id) {
TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
//判断删除的节点是否为父类
if(contentCategory.getIsParent()){
List<TbContentCategory> list=getContentCategoryListByParentId(id);
//递归删除
for(TbContentCategory tbContentCategory : list){
deleteContentCategory(contentCategory.getId());
}
}
//判断父类中是否还有子类节点,没有的话,把父类改成子类
if(getContentCategoryListByParentId(contentCategory.getParentId()).size()==1)
{
TbContentCategory parentCategory=contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
parentCategory.setIsParent(false);
contentCategoryMapper.updateByPrimaryKey(parentCategory);
}
contentCategoryMapper.deleteByPrimaryKey(id);
return TaotaoResult.ok();
}
/**
* 获取该节点下的孩子节点
* @param id
* @return 父节点下的所有孩子节点
*/
//通过父节点id来查询所有子节点,这是抽离出来的公共方法
private List<TbContentCategory> getContentCategoryListByParentId(long id){
TbContentCategoryExample example = new TbContentCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(id);
List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
return list;
}
三、controller层
就很简单了
如果没写,用”/content/category/delete”
@RequestMapping("/content/category/delete")
@ResponseBody
public TaotaoResult deleteContentCategory(Long id){
TaotaoResult result=contentCategoryService.deleteContentCategory(id);
return result;
}