Эх сурвалжийг харах

list转tree(注解方式)

WXF 3 жил өмнө
parent
commit
a2f689bed0

+ 11 - 0
api-common/src/main/java/api/common/pojo/annotation/WChildren.java

@@ -0,0 +1,11 @@
+package api.common.pojo.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface WChildren {
+}

+ 11 - 0
api-common/src/main/java/api/common/pojo/annotation/WId.java

@@ -0,0 +1,11 @@
+package api.common.pojo.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface WId {
+}

+ 11 - 0
api-common/src/main/java/api/common/pojo/annotation/WParentId.java

@@ -0,0 +1,11 @@
+package api.common.pojo.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.FIELD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface WParentId {
+}

+ 6 - 1
api-common/src/main/java/api/common/pojo/vo/system/MenuVO.java

@@ -1,5 +1,8 @@
 package api.common.pojo.vo.system;
 
+import api.common.pojo.annotation.WChildren;
+import api.common.pojo.annotation.WId;
+import api.common.pojo.annotation.WParentId;
 import lombok.Data;
 
 import java.util.List;
@@ -8,6 +11,7 @@ import java.util.List;
 public class MenuVO {
 
     //主键
+    @WId
     private String id;
     //菜单名称
     private String name;
@@ -16,9 +20,10 @@ public class MenuVO {
     //菜单排序
     private int sort;
     //父id
+    @WParentId
     private String parentId;
     //是否可见
     private String visible;
-
+    @WChildren
     private List<MenuVO> children;
 }

+ 76 - 0
api-common/src/main/java/api/common/util/ObjectUtil.java

@@ -1,9 +1,15 @@
 package api.common.util;
 
+import api.common.pojo.annotation.WChildren;
+import api.common.pojo.annotation.WId;
+import api.common.pojo.annotation.WParentId;
 import api.common.pojo.vo.system.DictVO;
 import com.fasterxml.jackson.databind.JsonNode;
 import org.springframework.beans.BeanUtils;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.security.PublicKey;
 import java.util.*;
 import java.util.function.BiConsumer;
@@ -146,4 +152,74 @@ public class ObjectUtil {
             }
         return parent;
     }
+
+    /**
+     * list转tree结构,<T>类型需要在属性上添加注解,标识树形结构:@WId 、@WParentId、 @WChildren;
+     * @param list
+     * @param <T>
+     * @return
+     */
+    public static <T> List<T> listToTree(List<T> list){
+        return listToTree(list,"0");
+    }
+
+    public static <T> List<T> listToTree(List<T> list,String root) {
+        try {
+            List<T> treeList = new LinkedList<T>();
+            Class<?> tClass = null;
+            for (T t : list) {
+                tClass = t.getClass();
+                break;
+            }
+            //反射解析Getter和Setter
+            Field[] fields = tClass.getDeclaredFields();
+            Method getId = null;
+            Method getParentId = null;
+            Method getChildren = null;
+            Method setChildren = null;
+            for (Field field:fields) {
+                String fieldName= field.getName();
+                String firstChar = fieldName.substring(0,1);
+                if(field.isAnnotationPresent(WId.class)){
+                    getId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
+                };
+                if(field.isAnnotationPresent(WParentId.class)){
+                    getParentId = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
+                };
+                if(field.isAnnotationPresent(WChildren.class)){
+                    getChildren = tClass.getMethod("get" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()));
+                    setChildren = tClass.getMethod("set" + fieldName.replaceFirst(firstChar,firstChar.toUpperCase()),List.class);
+                };
+            }
+            //解析列表
+            for (T node : list) {
+                if (root.equals((String)getParentId.invoke(node))) {
+                    treeList.add(findChildren(node, list,getId,getParentId,getChildren,setChildren));
+                }
+            }
+            return treeList;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return list;
+    }
+
+    /**
+     * list转tree递归调用
+     */
+    private static <T> T findChildren(T parent, List<T> list, Method getId, Method getParentId, Method getChildren , Method setChildren) throws InvocationTargetException, IllegalAccessException {
+        for (T node : list){
+            String id = (String)getId.invoke(parent);
+            String parentId = (String)getParentId.invoke(node);
+            if(id.equals(parentId)){
+                List<T> children = (List<T>) getChildren.invoke(parent);
+                if(ObjectUtil.isNull(children)){
+                    children = new ArrayList<T>();
+                    setChildren.invoke(parent,children);
+                }
+                children.add(findChildren(node, list,getId,getParentId,getChildren,setChildren));
+            }
+        }
+        return parent;
+    }
 }

+ 3 - 2
simulation-resource-server/src/main/java/com/css/simulation/resource/system/service/MenuService.java

@@ -18,7 +18,8 @@ public class MenuService {
     public List<MenuVO> getMenus() {
         String userId = AuthUtil.getCurrentUserId();
         List<MenuVO> menus = menuMapper.getMenus(userId);
-        List<MenuVO> treeList = ObjectUtil.listToTree(menus, "0", MenuVO::getId, MenuVO::getParentId, MenuVO::getChildren, MenuVO::setChildren);
-        return treeList;
+        //List<MenuVO> treeList = ObjectUtil.listToTree(menus, "0", MenuVO::getId, MenuVO::getParentId, MenuVO::getChildren, MenuVO::setChildren);
+        List<MenuVO> menuVOS = ObjectUtil.listToTree(menus);//注解方式转tree
+        return menuVOS;
     }
 }