获取树形结构的几种方式
2020-08-24 08:56
260 查看
获取树形结构的几种方式
一、存储过程实现
start with … connect by prior … 语句
示例
select connect_by_root(child_col) root, level , decode(connect_by_isleaf,0,'No',1,'Yes') is_leaf, sys_connect_by_path(child_col,'/') path from tree start with parent_col is null connect by prior child_col=parent_col
;
二、mybatis
xml文件实现(实际也是迭代的一种体现)
<resultMap type="com.test.mybatis.model.RoleInfo" id="roleModel"> <id column="id" property="roleid"/> <result column="name" property="rolename"/> <collection property="menulist" select="getMenu" column="id"> <!--当需要传递多个参数时使用{}--> </collection> </resultMap> <select id="getRoleInfo" resultMap="roleModel"> select id,name from role </select> <select id="getMenu" resultType="com.test.mybatis.model.Menu"> select m.id,m.name from menu m join roleandmenu ram on m.id=ram.menuId where ram.roleId=#{id} </select>
java 实体
import java.util.List; @Data public class RoleInfo { private Integer roleid; private String rolename; private List<Menu> menulist; }
三、迭代
1.先查出顶层,迭代查询子层
public List<TreeListDto> getInterfaceTree(List<TreeListDto> tldList, String interfaceId) { List<TreeListDto> tlds = new ArrayList<TreeListDto>(); for (TreeListDto tld1 : tldList) { if (MyStringUtil.isEquals(interfaceId, tld1.getParent())) { String id = tld1.getKey(); List<TreeListDto> tmpTree = this.getInterfaceTree(tldList, id); tld1.setChildren(tmpTree); if (tmpTree.size() <= 0) { tld1.setIsLeaf(true); } tlds.add(tld1); } } return tlds; }
相关文章推荐
- C语言获取当前系统时间的几种方式
- 获取spring的applicationContext的几种方式
- 获取MAC地址的几种方式
- Java获取键盘输入的几种方式
- java(JSP)中几种获取项目路径方式
- 从WebView获取图片的几种方式
- linux 远程获取服务器文件的几种方式
- C语言获取系统时间的几种方式
- Spring Boot获取前端页面参数的几种方式总结(二) ----
- C语言获取系统时间的几种方式
- 详解Android获取所有依赖库的几种方式
- Jquery 获取对象的几种方式
- springMVC 中几种获取request和response的方式
- 珍珠有哪几种获取方式?
- Jquery 获取对象的几种方式介绍
- 获取request对象的几种方式
- .net前端后台两种方式处理树形结构(tree)
- Java获取时间戳的几种方式
- 获取屏幕和View宽度和高度的几种方式
- asp.net C# 获取网页源码的几种方式