mybatis查询树状结构,代码实现

代码中实现
 @Override
    public List> getCrumbs(Long productTypeId) {
        List> result = new ArrayList<>();
        //1 通过productTypeId查ProductType,从里面获取path .1.2.3.
        ProductType productType = productTypeMapper.selectById(productTypeId);
        String path = productType.getPath();
        System.out.println(path);
        //2 分割path获取层级id集合,遍历每一级的id 1 2 3
        path = path.substring(1, path.length()-1); //1.2.3
        List ids = StrUtils.splitStr2LongArr(path,"\\.");
        System.out.println(ids);
        for (Long id : ids) {
            Map node = new HashMap<>();
            //3 使用遍历id构造一个节点
            //3.1 获取自己 通过id查询就ok
            ProductType owner = productTypeMapper.selectById(id);
            node.put("ownerProductType", owner);
            //3.2 通过自己pid,查询所有的儿子,再删除自己
            Long pid = owner.getPid();
            List parentAllChildren = productTypeMapper
                    .selectList(new EntityWrapper().eq("pid", pid));
            Iterator iterator = parentAllChildren.iterator();
            while (iterator.hasNext()){
                ProductType curent = iterator.next();
                if (curent.getId().longValue()== owner.getId().longValue()){
                    //父亲儿子中我,要干掉.
                    iterator.remove();
                    break;
                }
            }
            node.put("otherProductTypes", parentAllChildren);
            result.add(node);
        }
mybatis中实现
    
        
        
         children) 
                ofType="tableName"
                select="selectChildByParentId"
                column="id"/>
    
	
    

你可能感兴趣的:(mybatis查询树状结构,代码实现)