最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)菜單樹的示例代碼

 更新時間:2023年12月27日 16:39:45   作者:LonesomeRoad  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)菜單樹的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

使用ruoyi的菜單表結(jié)構(gòu)

實(shí)體類增加注解

實(shí)體類增加子菜單數(shù)組

    @TableField(exist = false)
    private List<Menu> children;

實(shí)現(xiàn)邏輯

    public List<Menu> selectMenuList(String menuName, Long userId) {
        //樹結(jié)構(gòu)
        List<Menu> menuList = null;
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(checkIsAdmin.checkIsAdmin(principal.getUser().getId())){
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(menuName),Menu::getMenuName, menuName);
            menuList = this.menuMapper.selectList(queryWrapper);
        }else{
            menuList = this.menuMapper.selectMenuListByUserId(menuName,userId);
        }
        List<Menu> finalMenuList = menuList;
        return menuList.stream()
                .filter(item -> Objects.equals(item.getParentId().toString(),"0"))
                .map(item -> item.setChildren(getChild(item.getId(), finalMenuList)))
                .sorted(Comparator.comparingInt(menu -> (menu.getShowSort() == null ? 0 : menu.getShowSort())))
                .collect(Collectors.toList());
    }
 
    private List<Menu> getChild(Long id, List<Menu> menuList){
        return menuList.stream()
                .filter(item -> Objects.equals(item.getParentId().toString(), id.toString()))
                .map(item -> item.setChildren(getChild(item.getId(), menuList)))
                .sorted(Comparator.comparingInt(menu -> (menu.getShowSort() == null ? 0 : menu.getShowSort())))
                .collect(Collectors.toList());
    }

結(jié)果展示

{
  "code": 200,
  "msg": "操作成功",
  "data": [
    {
      "id": "1731872513806221314",
      "menuName": "系統(tǒng)管理",
      "parentId": 0,
      "showSort": 1,
      "url": null,
      "reqPath": null,
      "isCache": "1",
      "target": null,
      "menuType": "M",
      "visible": "0",
      "isRefresh": null,
      "perms": null,
      "icon": "ep:add-location",
      "createTime": "2023-12-05 11:05:58",
      "updateTime": null,
      "operater": "qinyi",
      "componentName": null,
      "children": [
        {
          "id": "1731936951137632258",
          "menuName": "角色管理",
          "parentId": "1731872513806221314",
          "showSort": 1,
          "url": null,
          "reqPath": null,
          "isCache": "1",
          "target": null,
          "menuType": "C",
          "visible": "0",
          "isRefresh": null,
          "perms": null,
          "icon": "ep:alarm-clock",
          "createTime": "2023-12-05 15:22:01",
          "updateTime": null,
          "operater": "qinyi",
          "componentName": null,
          "children": []
        },
        {
          "id": "1734381007881097218",
          "menuName": "角色管理222",
          "parentId": "1731872513806221314",
          "showSort": 2,
          "url": null,
          "reqPath": null,
          "isCache": "1",
          "target": null,
          "menuType": "C",
          "visible": "0",
          "isRefresh": null,
          "perms": null,
          "icon": "ep:apple",
          "createTime": "2023-12-12 09:13:50",
          "updateTime": null,
          "operater": "qinyi",
          "componentName": null,
          "children": [
            {
              "id": "1734768479693627394",
              "menuName": "新增按鈕",
              "parentId": "1734381007881097218",
              "showSort": 1,
              "url": "",
              "reqPath": "",
              "isCache": "1",
              "target": null,
              "menuType": "F",
              "visible": "0",
              "isRefresh": null,
              "perms": null,
              "icon": "",
              "createTime": "2023-12-13 10:53:30",
              "updateTime": null,
              "operater": "qinyi",
              "componentName": null,
              "children": []
            }
          ]
        }
      ]
    },
    {
      "id": "1732203279467573249",
      "menuName": "菜單管理哦",
      "parentId": 0,
      "showSort": 2,
      "url": null,
      "reqPath": null,
      "isCache": "1",
      "target": null,
      "menuType": "C",
      "visible": "0",
      "isRefresh": null,
      "perms": null,
      "icon": "ep:camera-filled",
      "createTime": "2023-12-06 09:00:19",
      "updateTime": null,
      "operater": "qinyi",
      "componentName": null,
      "children": []
    }
  ]
}

這樣寫有點(diǎn)問題,就是模糊查詢的時候,查不到數(shù)據(jù),也就是過濾數(shù)據(jù)的時候子節(jié)點(diǎn)加載不到完整的樹結(jié)構(gòu),做以下改動

    /**
     * 在樹結(jié)構(gòu)上做模糊查詢(剪枝操作)
     */
    private List<Menu> getMenuByName(List<Menu> menuList,String selectName){
        Iterator<Menu> iterator = menuList.iterator();
        while(iterator.hasNext()){
            Menu menu = iterator.next();
            if(!menu.getMenuName().contains(selectName)){
                List<Menu> childrenList = menu.getChildren();
                if(!CollectionUtils.isEmpty(childrenList)){
                    getMenuByName(childrenList, selectName);
                }
                if(CollectionUtils.isEmpty(childrenList)){
                    iterator.remove();
                }
            }
        }
        return menuList;
    }

以上就是java實(shí)現(xiàn)菜單樹的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于java菜單樹的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java如何用Processing生成馬賽克風(fēng)格的圖像

    java如何用Processing生成馬賽克風(fēng)格的圖像

    這篇文章主要介紹了如何用java如何用Processing生成馬賽克風(fēng)格的圖像,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決

    springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決

    這篇文章主要介紹了springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題

    Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題

    當(dāng)進(jìn)行業(yè)務(wù)操作時,訂單發(fā)生異常 ,進(jìn)行了回滾操作,因?yàn)樵诓煌臄?shù)據(jù)庫實(shí)例中,余額卻扣除成功,此時發(fā)現(xiàn)數(shù)據(jù)不一致問題,本文給大家介紹Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務(wù)問題,感興趣的朋友一起看看吧
    2023-11-11
  • java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式

    java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式

    這篇文章主要介紹了java?數(shù)組越界判斷和獲取數(shù)組長度的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程

    SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程

    我們開發(fā)業(yè)務(wù)系統(tǒng)的時候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法

    String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法

    今天小編就為大家分享一篇String與XML互轉(zhuǎn)以及從XML取節(jié)點(diǎn)值并修改的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 教你用JAVA寫文本編輯器(三)

    教你用JAVA寫文本編輯器(三)

    這篇文章主要給大家介紹了關(guān)于用JAVA寫文本編輯器的相關(guān)資料,本文主要實(shí)現(xiàn)的是一個點(diǎn)擊選擇文本格式的窗口,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • Java方法引用原理實(shí)例解析

    Java方法引用原理實(shí)例解析

    這篇文章主要介紹了Java方法引用的原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • 一些常用Java命令及應(yīng)用總結(jié)

    一些常用Java命令及應(yīng)用總結(jié)

    這篇文章主要介紹了一些常用Java命令及應(yīng)用的相關(guān)資料,詳細(xì)講解了如何使用這些命令來排查Cpu和內(nèi)存飆高的問題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • 詳解Spring極速集成注解redis實(shí)錄

    詳解Spring極速集成注解redis實(shí)錄

    這篇文章主要介紹了詳解Spring極速集成注解redis實(shí)錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論

健康| 剑川县| 兖州市| 开化县| 织金县| 威信县| 科尔| 通渭县| 凤凰县| 左权县| 东丽区| 正安县| 新密市| 廊坊市| 宜兰市| 庆云县| 新巴尔虎右旗| 甘德县| 桂林市| 翁牛特旗| 达尔| 中方县| 繁峙县| 北流市| 新巴尔虎左旗| 若羌县| 凤台县| 竹北市| 大洼县| 海南省| 寿阳县| 弋阳县| 肥乡县| 治县。| 周至县| 榆社县| 双鸭山市| 甘孜县| 阜康市| 天津市| 阿鲁科尔沁旗|