构造菜单树

/ 0评 / 0

单纯的菜单好处理,直接从数据库把菜单遍历处理即可,但是现实情况往往是复杂的,>菜单是有可能是顶级菜单,也有可能是多级菜单,级数是不确定的,这个时候使用循环已经不够用了,所以,使用递归来完成。

菜单树
菜单树

先定义实体类,@Data是属于lombok注释,@TableName和@TableField是Mytabis-Plus的注释

@Data
@TableName
public class Menu{

       @Tabl
    private String id;
    // 菜单名称
    private String menuName;
    // 父菜单id
    private String parentId = "0";
    // 菜单url
    private String url;
    // 菜单图标
    private String icon;
    // 菜单顺序
    private int seqNum;
     /** @see MenuTypeEn
    
    @TableField(exist = false)
    private List<Menu> childMenus;
}

构造菜单树

先说 一下构造的思路:

@Service
public class MenuService extends ServiceImpl<MenuMapper, Menu> {

   @Autowired
    private MenuMapper menuMapper;

      public List<Menu> getSYSMenus() {
          List<Menu> rootMenu = menuMapper.selectList(null);
         List<Menu> menus = buildMenu(rootMenu);
            return menus;
 }

   private List<Menu> buildMenu(List<Menu> menus) {
                // 存放最后的结果
             List<Menu> list = new ArrayList<Menu>();
                
              // 先找到父级菜单
             for (Menu menu : menus) {
                     if (StrUtil.equals(menu.getParentId(), MenuConstant.MENU_PARENTID)) {
                         list.add(menu);
                       }
             }

           // 根据父级菜单的ID,去找子菜单
               for (Menu menu : list) {
                      menu.setChildMenus(getChild(menu.getId(), menus));
            }
             
              // 对结果进行排序
             sortSeqNum(list);
             return list;
  }

   /**
    * 递归查找子元素
      * 
    * @param id
   * @param menus
        * @return
     */
   private List<Menu> getChild(String id, List<Menu> menus) {
              // 存放子菜单集合
             List<Menu> childMenus = new LinkedList<Menu>();
         for (Menu menu : menus) {
                     // 如果当前菜单的父级菜单ID与传进来的ID一直,则添加进子菜单集合
                       if (StrUtil.equals(id, menu.getParentId())) {
                         childMenus.add(menu);
                 }
             }

           // 再次>循环子菜单集合,找出子菜单的子菜单
            for (Menu menu : childMenus) {
                        // 递归查找
                   menu.setChildMenus(getChild(menu.getId(), menus));
            }

           return childMenus;
    }

   /**
    *      对菜单排序
     * 
    * @param menus
        */
   public void sortSeqNum(List<Menu> menus) {
         for (Menu menu : menus) {
                     List<Menu> chilMenus = menu.getChildMenus();
                       if (CollectionUtil.isNotEmpty(chilMenus)) {
                           sortSeqNum(chilMenus);
                        } else {
                              menus.sort((o1, o2) -> o1.getSeqNum() - o2.getSeqNum());
                      }
             }
     }
}

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogApplicationTests {
 
      @Autowired
    private MenuService menuService;
      
      @Test
 public void getSYSMenus() {
           List<Menu> menus = menuService.getSYSMenus();
              System.out.println(JSONUtil.parseArray(menus));
       }
}

输出结果,结果出来的,可能与我实体类定义的多出了一些属性,不过那都不重要的

[{
    "createTime": 1565483110000,
        "seqNum": 0,
        "isDelete": false,
  "icon": "fe fe-home",
     "childMenus": [],
   "menuName": "首页",
       "id": "faf101522f4345fca6e6a41fab916adc",
 "type": 1,
  "parentId": "0",
  "url": "/admin/index"
}, {
       "createTime": 1565487201000,
        "seqNum": 1,
        "isDelete": false,
  "icon": "fe fe-box",
      "childMenus": [{
            "createTime": 1565487303000,
                "seqNum": 0,
                "isDelete": false,
          "childMenus": [],
           "menuName": "所有文章",
           "id": "be815a16ca54451a8b6077a35608a10f",
         "type": 1,
          "parentId": "dc64ad2a0708411aa90d3d82541fbac4",
           "url": ""
 }, {
          "createTime": 1565487362000,
                "seqNum": 1,
                "isDelete": false,
          "childMenus": [],
           "menuName": "发布文章",
           "id": "963d612619ba457b9ab2c5452ce52577",
         "type": 1,
          "parentId": "dc64ad2a0708411aa90d3d82541fbac4",
           "url": ""
 }],
   "menuName": "文章管理",
   "id": "dc64ad2a0708411aa90d3d82541fbac4",
 "type": 1,
  "parentId": "0",
  "url": "#"
}, {
  "createTime": 1565487408000,
        "seqNum": 2,
        "isDelete": false,
  "icon": "fe fe-box",
      "childMenus": [{
            "createTime": 1565487443000,
                "seqNum": 0,
                "isDelete": false,
          "icon": "fe fe-box",
              "childMenus": [],
           "menuName": "所有分类",
           "id": "3c27ee2fc0584554a3fa8046af978738",
         "type": 1,
          "parentId": "f005223f42b34f5dba063d7a613ddb88",
           "url": "#"
        }, {
          "createTime": 1565487480000,
                "seqNum": 1,
                "isDelete": false,
          "childMenus": [],
           "menuName": "所有标签",
           "id": "25697fbe35a147558f43f86156f2d6bd",
         "type": 1,
          "parentId": "f005223f42b34f5dba063d7a613ddb88",
           "url": ""
 }],
   "menuName": "分类标签",
   "id": "f005223f42b34f5dba063d7a613ddb88",
 "type": 1,
  "parentId": "0",
  "url": "#"
}, {
  "createTime": 1565487551000,
        "seqNum": 3,
        "isDelete": false,
  "icon": "fe fe-box",
      "childMenus": [{
            "createTime": 1565487585000,
                "seqNum": 0,
                "isDelete": false,
          "childMenus": [],
           "menuName": "所有页面",
           "id": "1699896f1a3d4b3dbd2bef669c7c7774",
         "type": 1,
          "parentId": "167cebb5c71942e0bca54b02762e18b7",
           "url": ""
 }, {
          "createTime": 1565487622000,
                "seqNum": 1,
                "isDelete": false,
          "childMenus": [],
           "menuName": "添加页面",
           "id": "6d23ae7906104ffc89fd4d20d198d210",
         "type": 1,
          "parentId": "167cebb5c71942e0bca54b02762e18b7",
           "url": ""
 }],
   "menuName": "页面管理",
   "id": "167cebb5c71942e0bca54b02762e18b7",
 "type": 1,
  "parentId": "0",
  "url": "#"
}, {
  "createTime": 1565487664000,
        "seqNum": 4,
        "isDelete": false,
  "icon": "fe fe-box",
      "childMenus": [{
            "createTime": 1565487705000,
                "seqNum": 0,
                "isDelete": false,
          "childMenus": [],
           "menuName": "所有评论",
           "id": "e402146b0f774cd08e1ecb9408dfdeea",
         "type": 1,
          "parentId": "eda1698393964ccd983d8915df976588",
           "url": ""
 }],
   "menuName": "评论管理",
   "id": "eda1698393964ccd983d8915df976588",
 "type": 1,
  "parentId": "0",
  "url": "#"
}, {
  "createTime": 1565483193000,
        "seqNum": 5,
        "isDelete": false,
  "icon": "fe fe-file",
     "childMenus": [{
            "createTime": 1565483253000,
                "seqNum": 0,
                "isDelete": false,
          "childMenus": [],
           "menuName": "网站设置",
           "id": "2492559165db4c00a17d5573af1772f8",
         "type": 1,
          "parentId": "5122e8e8b62a4408b5a0fbc059228870",
           "url": "/admin/website"
   }],
   "menuName": "系统设置",
   "id": "5122e8e8b62a4408b5a0fbc059228870",
 "type": 1,
  "parentId": "0",
  "url": "#"
}]

发表评论

电子邮件地址不会被公开。 必填项已用*标注