{"id":8978,"date":"2024-05-18T21:01:01","date_gmt":"2024-05-18T13:01:01","guid":{"rendered":""},"modified":"2024-05-18T21:01:01","modified_gmt":"2024-05-18T13:01:01","slug":"Java\u4ee3\u7801\u5b9e\u73b0\u9879\u76ee\u4e24\u7ea7\u83dc\u5355\u5c55\u793a\uff08\u56fe\u6587\u6559\u7a0b\uff09","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8978.html","title":{"rendered":"Java\u4ee3\u7801\u5b9e\u73b0\u9879\u76ee\u4e24\u7ea7\u83dc\u5355\u5c55\u793a\uff08\u56fe\u6587\u6559\u7a0b\uff09"},"content":{"rendered":"

\u672c\u5fae\u535a\u53ea\u9002\u7528\u4e8e\u4e24\u5c42\u7684\u83dc\u5355\u5c55\u793a\uff0c\u5199\u4f5c\u7684\u521d\u8877\u662f\u4e3a\u4e86\u4ecb\u7ecd\u9012\u5f52\u505a\u94fa\u57ab\u3002<\/p>\n

\u8be6\u7ec6\u8bf4\u660e\u90fd\u5199\u5728\u4e86\u4ee3\u7801\u7684\u6ce8\u91ca\u4e0a\uff0c\u4e0d\u505a\u5355\u72ec\u8bf4\u660e\u4e86\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n

1.\u6570\u636e\u5e93\u8bbe\u8ba1<\/h4>\n

\"Java\u4ee3\u7801\u5b9e\u73b0\u9879\u76ee\u4e24\u7ea7\u83dc\u5355\u5c55\u793a\uff08\u56fe\u6587\u6559\u7a0b\uff09<\/p>\n

\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u7ec3\u4e60\uff0c\u63d0\u4f9b\u5efa\u8868SQL\u5982\u4e0b\uff1a<\/strong><\/p>\n

-- ----------------------------\n-- Table structure for sys_menu\n-- ----------------------------\nDROP TABLE IF EXISTS `sys_menu`;\nCREATE TABLE `sys_menu` (\n  `menu_id` bigint(20) NOT NULL AUTO_INCREMENT,\n  `parent_id` bigint(20) DEFAULT NULL COMMENT '\u7236\u83dc\u5355ID\uff0c\u4e00\u7ea7\u83dc\u5355\u4e3a0',\n  `name` varchar(50) DEFAULT NULL COMMENT '\u83dc\u5355\u540d\u79f0',\n  `url` varchar(200) DEFAULT NULL COMMENT '\u83dc\u5355URL',\n  `perms` varchar(500) DEFAULT NULL COMMENT '\u6388\u6743(\u591a\u4e2a\u7528\u9017\u53f7\u5206\u9694\uff0c\u5982\uff1auser:list,user:create)',\n  `type` int(11) DEFAULT NULL COMMENT '\u7c7b\u578b   0\uff1a\u76ee\u5f55   1\uff1a\u83dc\u5355   2\uff1a\u6309\u94ae',\n  `icon` varchar(50) DEFAULT NULL COMMENT '\u83dc\u5355\u56fe\u6807',\n  `order_num` int(11) DEFAULT NULL COMMENT '\u6392\u5e8f',\n  `gmt_create` datetime DEFAULT NULL COMMENT '\u521b\u5efa\u65f6\u95f4',\n  `gmt_modified` datetime DEFAULT NULL COMMENT '\u4fee\u6539\u65f6\u95f4',\n  PRIMARY KEY (`menu_id`)\n) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8 COMMENT='\u83dc\u5355\u7ba1\u7406';<\/code><\/pre>\n

2.\u5b9e\u4f53\u7c7bMenuDO<\/h4>\n

\u8be5\u5b9e\u4f53\u7c7b\u662f\u5728\u5de5\u4f5c\u4e2d\u603b\u7ed3\u51fa\u6765\u7684\uff0c\u5b57\u6bb5\u8f83\u5168\uff0c\u9002\u7528\u573a\u666f\u4e5f\u6bd4\u8f83\u591a\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u6700\u4e3b\u8981\u7684\u5b57\u6bb5\u53ea\u6709\u524d\u51e0\u4e2a\uff1amenuId\u3001parentId\u3001name\u3001url\u3002<\/p>\n

import java.io.Serializable;\nimport java.util.Date;\n\npublic class MenuDO implements Serializable {\n    private static final long serialVersionUID = 1L;\n    \/\/ \u4e3b\u952e\n    private Long menuId;\n    \/\/ \u7236\u83dc\u5355ID\uff0c\u4e00\u7ea7\u83dc\u5355\u4e3a0\n    private Long parentId;\n    \/\/ \u83dc\u5355\u540d\u79f0\n    private String name;\n    \/\/ \u83dc\u5355URL\n    private String url;\n    \/\/ \u6388\u6743(\u591a\u4e2a\u7528\u9017\u53f7\u5206\u9694\uff0c\u5982\uff1auser:list,user:create)\n    private String perms;\n    \/\/ \u7c7b\u578b 0\uff1a\u76ee\u5f55 1\uff1a\u83dc\u5355 2\uff1a\u6309\u94ae\n    private Integer type;\n    \/\/ \u83dc\u5355\u56fe\u6807\n    private String icon;\n    \/\/ \u6392\u5e8f\n    private Integer orderNum;\n    \/\/ \u521b\u5efa\u65f6\u95f4\n    private Date gmtCreate;\n    \/\/ \u4fee\u6539\u65f6\u95f4\n    private Date gmtModified;\n\n    ......\n    \/\/ \u7701\u7565get()\u3001set()\u7b49\u65b9\u6cd5\n}<\/code><\/pre>\n

3.\u4e1a\u52a1\u7c7bTree<T><\/h4>\n
public class Tree<T> {\n    \/\/ \u8282\u70b9ID\n    private String id;\n    \/\/ \u663e\u793a\u8282\u70b9\u6587\u672c\n    private String text;\n    \/\/ \u8282\u70b9\u72b6\u6001\uff0copen closed\n    private Map<String, Object> state;\n    \/\/ \u8282\u70b9\u662f\u5426\u88ab\u9009\u4e2d true false\n    private boolean checked = false;\n    \/\/ \u8282\u70b9\u5c5e\u6027\n    private Map<String, Object> attributes;\n    \/\/ \u8282\u70b9\u7684\u5b50\u8282\u70b9\n    private List<Tree<T>> children = new ArrayList<Tree<T>>();\n    \/\/ \u7236ID\n    private String parentId;\n    \/\/ \u662f\u5426\u6709\u7236\u8282\u70b9\n    private boolean hasParent = false;\n    \/\/ \u662f\u5426\u6709\u5b50\u8282\u70b9\n    private boolean hasChildren = false;\n\n    ......\n    \/\/ \u7701\u7565get()\u3001set()\u7b49\u65b9\u6cd5\n}<\/code><\/pre>\n

4.service\u5c42\u8c03\u7528\u7684\u65b9\u6cd5<\/h4>\n

\u8fd4\u56de\u7684list\u7ed3\u679c\uff0c\u5c31\u662f\u6ee1\u8db3\u903b\u8f91\u7684tree\u578b\u6570\u636e\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7ed9\u9875\u9762\u4f7f\u7528\u3002<\/p>\n

    public List<Tree<MenuDO>> listMenuTree() {\n        String idParam = \"0\";\t\t                           \/\/ \u81ea\u5b9a\u4e49\u9876\u7ea7\u7ed3\u70b9\n        List<Tree<MenuDO>> trees = new ArrayList<Tree<MenuDO>>();  \/\/ \u5b58\u653e\u7ed3\u679c\n        List<MenuDO> menuDOs = menuMapper.queryList();             \/\/ \u53d6\u51fa\u6570\u636e\n\n        \/\/ 1. \u521d\u6b65\u5904\u7406\uff1a\u8ba9\u539f\u59cb\u6570\u636e\u5177\u5907\u6211\u4eec\u81ea\u5b9a\u7684\u7684tree\u7684\u7279\u5f81\n        for (MenuDO sysMenuDO : menuDOs) {\n            Tree<MenuDO> tree = new Tree<MenuDO>();\n            tree.setId(sysMenuDO.getMenuId().toString());\n            tree.setParentId(sysMenuDO.getParentId().toString());\n            tree.setText(sysMenuDO.getName());\n            Map<String, Object> attributes = new HashMap<>(16);\n            attributes.put(\"url\", sysMenuDO.getUrl());\n            attributes.put(\"icon\", sysMenuDO.getIcon());\n            tree.setAttributes(attributes);\n            trees.add(tree);\n        }\n\n        \/\/ 2. \u9012\u5f52\u5904\u7406\uff1a\u6839\u636e\u6570\u636e\u5e93\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u9876\u7ea7\u7ed3\u70b9\u7684parentId\u503c\uff0c\u9ed8\u8ba4\u4e3a0\n        List<Tree<MenuDO>> list = BuildTree.buildList(trees, idParam);\n\n        return list;\n    }<\/code><\/pre>\n

5.util\u7c7b<\/h4>\n
import java.util.ArrayList;\nimport java.util.HashMap;\nimport java.util.List;\nimport java.util.Map;\n\n\/**\n * \u9012\u5f52\u6811\u5f62\u7ed3\u6784\u7c7b\n *\/\npublic class BuildTree {\n\n    \/**\n     * \u81ea\u5b9a\u4e49\u9876\u7ea7\u7ed3\u70b9\u7684parentId\uff0c\u8fd4\u56de\u9012\u5f52\u6811\u5f62\u7ed3\u6784\n     *\/\n    public static <T> List<Tree<T>> buildList(List<Tree<T>> nodes, String idParam) {\n        \/\/ 1.\u975e\u7a7a\u5224\u65ad\n        if (nodes == null) {\n            return null;\n        }\n\n        \/\/ 2.\u5b9a\u4e49\u8fd4\u56de\u6570\u636e\u7c7b\u578b\n        List<Tree<T>> topNodes = new ArrayList<Tree<T>>();\n\n        \/\/ 3.\u53d6\u51fa\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5224\u65ad\u5b83\u6709\u6ca1\u6709\u7236\u7c7b\n        for (Tree<T> children : nodes) {\n            String pid = children.getParentId();\n\n            \/\/ 3.1 pid\uff08parentId\uff09\u4e3a\u7a7a\uff0c\u6216\u8005\u7b49\u4e8e\u7236\u8282\u70b9\uff0c\u5219\u6ca1\u6709\u7236\u7c7b\uff0c\u76f4\u63a5\u8fd4\u56de\n            if (pid == null || idParam.equals(pid)) {\n                topNodes.add(children);\n                continue;\n            }\n\n            \/\/ 3.2 \u5426\u5219\uff0c\u904d\u5386\u4e00\u904d\u96c6\u5408\uff0c\u627e\u5b83\u7684\u7236\u7c7b\uff0c\u539f\u5219\uff1a\u5b50\u7c7b\u7684parentId = \u7236\u7c7b\u7684id\n            for (Tree<T> parent : nodes) {\n                String id = parent.getId();\n                if (id != null && id.equals(pid)) {\n\n                    \/\/ 3.2.1 \u5c06\u5b50\u7c7b\u6dfb\u52a0\u5230\u7236\u7c7b\u7684children\u5c5e\u6027\u4e0b\n                    parent.getChildren().add(children);\n\n                    \/\/ 3.2.2 \u8bbe\u7f6e\u5b50\u8282\u70b9\u95ed\u5408\u72b6\u6001\uff1atrue - \u5173\u95ed\n                    children.setHasParent(true);\n                    parent.setChildren(true);\n                }\n            }\n        }\n\n        \/\/ 4. \u8fd4\u56de\u7ed3\u679c\u96c6\n        return topNodes;\n    }\n\n}<\/code><\/pre>\n

6.\u7ed3\u679c\u5c55\u793a<\/h4>\n

\u57fa\u4e8eBootDo\u6210\u719f\u7684\u67b6\u6784\uff0c\u5c0f\u7f16\u81ea\u5df1\u642d\u5efa\u4e86\u4e00\u4e2a\u5f00\u53d1\u6846\u67b6\uff0c\u4f7f\u7528\u9012\u5f52\u5c55\u793a\u83dc\u5355\u680f\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n

\"Java\u4ee3\u7801\u5b9e\u73b0\u9879\u76ee\u4e24\u7ea7\u83dc\u5355\u5c55\u793a\uff08\u56fe\u6587\u6559\u7a0b\uff09<\/p>\n

 <\/p>\n

\u66f4\u591a\u7cbe\u5f69\uff0c\u8bf7\u5173\u6ce8\u6211\u7684\"\u4eca\u65e5\u5934\u6761\u53f7\"\uff1aJava\u4e91\u7b14\u8bb0
\u968f\u65f6\u968f\u5730\uff0c\u8ba9\u4f60\u62e5\u6709\u6700\u65b0\uff0c\u6700\u4fbf\u6377\u7684\u638c\u4e0a\u4e91\u670d\u52a1<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"Java\u4ee3\u7801\u5b9e\u73b0\u9879\u76ee\u4e24\u7ea7\u83dc\u5355\u5c55\u793a\uff08\u56fe\u6587\u6559\u7a0b\uff09\u672c\u5fae\u535a\u53ea\u9002\u7528\u4e8e\u4e24\u5c42\u7684\u83dc\u5355\u5c55\u793a\uff0c\u5199\u4f5c\u7684\u521d\u8877\u662f\u4e3a\u4e86\u4ecb\u7ecd\u9012\u5f52\u505a\u94fa\u57ab","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8978"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8978"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8978\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}