获取树形结构的几种方式

获取树形结构的几种方式

一、存储过程实现
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值