淘淘商城內容管理刪除
阿新 • • 發佈:2019-02-17
一、分析
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;
}