递归删除资源树 Ztree
前言
- 最近项目里有这么一个需求:现在有一个用Ztree编写的资源树,当删除资源树的某个节点时,则将此节点下面的所有节点全部删除,这里显然就用到了递归;若此节点被删除后无其它的兄弟节点了,我们还需要将其父节点更新成新的子节点。
代码中用到的技术
- 小编操作数据库用的是mybatis,大部分操作直接使用的mybatis的逆向工程,至于mapper的注入,我就不贴代码了。
1、删除节点的入口
public void deleteCategory(Long id) {
//将此节点对象从数据库中搜出来
TbContentCategory node = categoryMapper.selectByPrimaryKey(id);
//删除此节点
this.recursiveDelete(id);
//判断是否更新父节点
this.updateParentNode(node.getParentId());
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2、递归删除资源树上的节点
/**
* 递归删除资源树上的节点
* @param 要删除的节点id
*/
public void recursiveDelete(Long id) {
// 查询此节点下面的所有的子节点
List<TbContentCategory> list = getListByParentId(id);
// 若此节点下面没有子节点
if (list.size() == 0) {
TbContentCategory deleteNode = categoryMapper.selectByPrimaryKey(id);
//得到此节点的父节点Id
Long parentId = deleteNode.getParentId();
//删除此节点
categoryMapper.deleteByPrimaryKey(id);
//删除此节点后,判断此节点的父节点是否为子节点,若是,则更新其父节点为子节点
this.updateParentNode(parentId);
} else {
categoryMapper.deleteByPrimaryKey(id);
for (TbContentCategory category : list) {
if (category.getIsParent()) {
//递归删除节点
this.recursiveDelete(category.getId());
} else { categoryMapper.deleteByPrimaryKey(category.getId());
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
3、删除子节点后,判断其父节点是否需要更新成子节点
/**
* 判断此节点是否存在兄弟节点,若不存在,则将其父节点更新成子节点
* @param 节点Id
*/
private void updateParentNode(Long parentId) {
//查询此节点的所有的兄弟节点
List<TbContentCategory> contentCat = getListByParentId(parentId);
//若无兄弟节点
if (contentCat.size() == 0) {
//更新此节点的父节点为子节点
TbContentCategory node2 = categoryMapper.selectByPrimaryKey(parentId);
node2.setIsParent(false);
categoryMapper.updateByPrimaryKeySelective(node2);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
4、根据父节点Id 查询所有的兄弟节点
/**
* 根据父节点Id 查询所有的兄弟节点
* @param parentId
* @return
*/
public List<TbContentCategory> getListByParentId(Long parentId) {
TbContentCategoryExample example = new TbContentCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId);
List<TbContentCategory> list = categoryMapper.selectByExample(example);
return list;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
递归删除资源树 Ztree的更多相关文章
- 多层树级关系的json,递归删除空值的数据
data =[{ "name": "省", "children":[ { "name": "市区", ...
- [译]聊聊C#中的泛型的使用(新手勿入) Seaching TreeVIew WPF 可编辑树Ztree的使用(包括对后台数据库的增删改查) 字段和属性的区别 C# 遍历Dictionary并修改其中的Value 学习笔记——异步 程序员常说的「哈希表」是个什么鬼?
[译]聊聊C#中的泛型的使用(新手勿入) 写在前面 今天忙里偷闲在浏览外文的时候看到一篇讲C#中泛型的使用的文章,因此加上本人的理解以及四级没过的英语水平斗胆给大伙进行了翻译,当然在翻译的过程中发 ...
- C#递归生成HTML树,C#递归生成xml树
C#递归生成HTML树 public StringBuilder str = new StringBuilder(); //定义一个字符串 private void get_navigation_ ...
- time | sys | os 模块,递归删除文件,项目分析
一,复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块 ...
- 【坑】Java中遍历递归删除List元素
运行环境 idea 2017.1.1 需求背景 需要做一个后台,可以编辑资源列表用于权限管理 资源列表中可以有父子关系,假设根节点为0,以下以(父节点id,子节点id)表示 当编辑某个资源时,需要带出 ...
- day18 时间:time:,日历:calendar,可以运算的时间:datatime,系统:sys, 操作系统:os,系统路径操作:os.path,跨文件夹移动文件,递归删除的思路,递归遍历打印目标路径中所有的txt文件,项目开发周期
复习 ''' 1.跨文件夹导包 - 不用考虑包的情况下直接导入文件夹(包)下的具体模块 2.__name__: py自执行 '__main__' | py被导入执行 '模块名' 3.包:一系列模块的集 ...
- python 递归删除空文件夹
Python如何递归删除空文件夹 1.Python如何递归删除空文件夹,这个问题很常见.但大多数人的解决办法都是自己实现递归函数解决这个问题,其实根本不用那么麻烦.Python中的os.walk提供了 ...
- python dict clear只能删除一层,不能够递归删除。
void PyDict_Clear(PyObject *op) { dictobject *mp; dictentry *ep, *table; int table_is_malloced; Py_s ...
- 【File】递归删除文件夹中子级文件/夹,并删除文件夹
今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...
随机推荐
- 树莓派/RaspberryPi 内核源码下载
树莓派的源码有两种下载方式:压缩包下载和git clone指令下载. 1.压缩包下载 选择对应分支,点击Github界面的 下载按钮即可,如下图: 测试发现,同样的分支,用压缩包方式下载后编译会出错, ...
- C#:xml操作(待补充)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- 有道翻译 / 百度翻译Api
比较推荐使用百度翻译api 不推荐有道翻译,比较水. http://ai.youdao.com/docs/doc-trans-api.s#p02 http://ai.youdao.com/docs/d ...
- atitit.新增编辑功能 跟orm的实现 attilax p31
atitit.新增编辑功能 跟orm的实现 attilax p31 1. 流程的实现 1 2. view的实现(dwr) 1 3. 获取表结构 1 4. grep filt req params 2 ...
- 基于FPGA的PCIe接口实现(具体讲解了数据流向)
时间:2014-12-09 来源:西安电子科技大学电子工程学院 作者:姜 宁,陈建春,王 沛,石 婷 摘要 PCI Express是一种高性能互连协议,被广泛应用于网络适配.图形加速器.网络存储.大数 ...
- Delphi 全局画点TCanvas.Pixels[X,Y]
procedure TForm1.btnChangePixelClick(Sender: TObject); var baseX : integer ; baseY : integer ; i,j : ...
- SpringBoot接口服务处理Whitelabel Error Page
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50915979 <SpringBoot接口服务处理Whitelabel Erro ...
- How to Install Xcode, Homebrew, Git, RVM, Ruby & Rails on Snow Leopard, Lion, Mountain Lion, and Mavericks
After following many outdated and incomplete instructions for setting up a web development environme ...
- QQ连连看-外挂
QQ连连看-外挂 2014-11-06 参考 [1] [视频教程] c语言实践课程之qq连连看辅助开发 [2] CE工具下载 [3] [原创]qq连连看外挂制作详解
- ubuntu和pypi换源
ubuntu用apt-get下载的源是可以更换的.之前一直是打开软件中心在编辑里找源,找到后系统会自动备份原来的源并换源.奇怪却搜不到自己学校的源=.= 想换源还有一个原因,之前在update的时候会 ...