递归树结构封装

 

递归sql

select t3.* from (
select t1.*,
if(find_in_set(pid, @pids) > 0, @pids := concat(@pids, ',', id),
if (t1.id = @pids,@pids,0)) as ischild
from (
select <include refid="columns"/>,cast(shape as char(1000) character set utf8)shapeData from t3d_wss_port t
) t1,
(select @pids := #{id}) t2
) t3 where ischild != 0 order by ischild, id

递归还原

@PostMapping(value = "/Hy",produces = "application/json;charset=utf-8")
public Object Hy(){
List<T3dWssPort> data = this.wssPortMapper.getPortTree(1);
List<Map<String, Object>> tree = getree(data, 0,0);
List<Map<String, Object>> hy = getHy(tree, 0);
System.out.println(hy);
return 1;
}


private static List<Map<String, Object>> getHy(List<Map<String, Object>> data, Integer parentId){
List<Map<String, Object>> l=new ArrayList<>();
for (Map<String, Object> stringObjectMap : data) {
if(parentId == stringObjectMap.get("pid")){
boolean children = stringObjectMap.containsKey("children");
if (children) {
if(!ObjectUtils.isEmpty(stringObjectMap.get("children")))
getHy((List) stringObjectMap.get("children"), Integer.valueOf(stringObjectMap.get("id")+""));
}
stringObjectMap.entrySet().forEach((e)->{
String key = e.getKey();
Object value = e.getValue();
if(!key.equalsIgnoreCase("children")&&key.contains("pid")){
System.out.println(value+"====="+parentId);
}
});

}
}
return l;

}

 

/**
* 递归控制层级
*/
 

@PostMapping(value = "/Port",produces = "application/json;charset=utf-8")
public Object pselect(@RequestBody JSONObject object){
List<Map> objects = Lists.newArrayList();
List<Map> in = object.getObject("IN", List.class);
in.forEach(item->{
Map map=new HashMap();
int id =(int) item.get("ID");
int level =(int) item.get("Level");
LEV=level;
int pid = this.portService.getOne(new QueryWrapper<T3dWssPort>().eq("id", id)).getPid();
List<T3dWssPort> data = this.wssPortMapper.getPortTree(id);
List<Map<String, Object>> tree = getree(data, pid,0);
map.put("tree_id_"+id,tree);
objects.add(map);
});
return objects;
}


private static List<Map<String, Object>> getree(List<T3dWssPort> data, Integer parentId, int count){
if(LEV>=count||LEV<0) {
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getPid().equals(parentId)) {
try {
Map map = PropertyUtils.describe(data.get(i));
count=i+1;
List<Map<String, Object>> ds = getree(data, data.get(i).getId(),count);//i
if(!CollectionUtils.isEmpty(ds)){
map.put("children", ds);
}
list.add(map);
} catch (Exception e) {
e.printStackTrace();
}

}
}
return list;
}
return null;
}

 

/**
* 根据父级节点获取所有的子集节点
* @param parentNode
* @param allList
* @return
*/
public static List<Map<String, Object>> getJava8Children(Map<String,Object> parentNode, List<Map<String, Object>> allList){
return allList.stream()
.filter(curNode -> ObjectUtils.isNotEmpty(curNode.get("parentId")) && Objects.equals(curNode.get("parentId"),parentNode.get("id")))
.peek(m -> m.put("children", getJava8Children(m,allList))).collect(Collectors.toList());
}

 

 

 

 

public class Sheng {

private String name;

private int id;

private Integer pid;

private static List<Sheng> shenList;
private List<Shi> shiList;
private List<Xian> xianList;
static {
shenList=new ArrayList() {{
add(new Sheng("河南省",1,0));
add(new Sheng("郑州市",2,1));
add(new Sheng("开封市",3,1));
add(new Sheng("洛阳市",4,1));
add(new Sheng("洛阳市-1",5,4));
add(new Sheng("洛阳市-2",6,4));
add(new Sheng("洛阳市-3",7,4));
add(new Sheng("洛阳市-4",8,4));
}};
}

public static List<Map<String, Object>> bulidTree(List<Sheng> sheng, int pid_) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (Sheng item : sheng) {
Sheng shen = item;
//第一次temp.getParentId()=-1 parentId=-1 判断进入重新递归方法
//第二次temp.getParentId()=-1 parentId=1 不递归不进判断
//第二次temp.getParentId()=2 parentId=1 不递归不进判断
if (pid_ == shen.getPid()) {
Map<String, Object> map = new HashMap<>();
map.put("menuName", shen.getName());
map.put("menuIcon", "icon");
map.put("menuUrl", "login/test");
map.put("subMenus", bulidTree(sheng, shen.getId()));
list.add(map);
}
}
return list;
}

public static void main(String[] args) {
Sheng sheng = new Sheng();
System.out.println(bulidTree(sheng.getShenList(), 0));
}
}

https://blog.csdn.net/qq_36476972/article/details/75089927

posted @ 2019-06-02 14:42  小农_码  阅读(841)  评论(0编辑  收藏  举报