递归删除资源树 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】递归删除文件夹中子级文件/夹,并删除文件夹
今天有这样一个需求,需要删除某一个文件夹,但是文件夹中还有子级的文件 或者还可能会有文件夹在里面,所以就需要使用一个简单的递归才能将文件夹删除成功,包括文件夹中的子级文件/夹.!!! 其实很简单,就一 ...
随机推荐
- SQLServer 2008中SQL增强之三 Merge(在一条语句中使用
SQLServer 2008中SQL增强之三 Merge(在一条语句中使用Insert,Update,Delete) SQL Server 2008提供了一个增强的SQL命令Merge,用法参看M ...
- jquery-osx
jQuery OSX https://github.com/jelly-liu/jquery-osx jquery-osx jquery, desktop, jquery desktop, jquer ...
- NAT Network Address Translation,网络地址转换
Network Address Translation,网络地址转换
- StrongLoop
http://loopback.io/getting-started/ 使用 StrongLoop 创建 Node.js MySQL 应用程序 StrongLoop 是 IBM 的一家子公司,Stro ...
- MongoDB Query 的几个方法
Query.All("name", "a", "b");//通过多个元素来匹配数组Query.And(Query.EQ("name ...
- form表单回车Enter不直接提交,类似tab切换
<input> 控件增加onkeypress事件 onkeypress="return handleEnter(this, event)" JS如下: var keyC ...
- XCode5中新建工程后强制使用了ARC,如何去掉?
打开你的工程,点击目录的工程文件,最顶端蓝色的,然后选择project下你的工程,还是蓝色那项,然后build Settings,然后往下拉,在Apple LLVM 5.0 - Language - ...
- 移动touch事件之一
触摸事件分类: touchstart:当手指触摸屏幕时触发:即使已经有一个手指放在了屏幕上也会触发. touchmove:当手指在屏幕上滑动时连续的触发.在这个事件发生期间,调用preventDefa ...
- web中文字体Font-family应该写什么?
最佳实践是: font-family: Helvetica, Tahoma, Arial, "Microsoft YaHei", "微软雅黑",STXihei, ...
- SAP ECC6安装系列二:安装前的准备工作
原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 安装 Java 1,安装 Java, ...