美文网首页
树形菜单

树形菜单

作者: 半日孤独 | 来源:发表于2021-07-28 14:44 被阅读0次

    实现方式

    1.通过mybatis编写xml

        <resultMap id="BaseResultMap" type="com.gy.hd.common.vo.MenuRoleVO">
            <id property="menuId" column="menu_id"/>
            <result column="menu_name" property="menuName"/>
            <result column="menu_url" property="menuUrl"/>
            <result column="parent_id" property="parentId"/>
            <result column="menu_type" property="menuType"/>
            <result column="menu_icon" property="menuIcon"/>
            <!--单个参数column="menu_id",多个参数column="{menuId=menu_id,cc=cc}"-->
            <collection property="menuList" ofType="com.gy.hd.common.vo.MenuRoleVO" select="com.gy.hd.user.dao.RoleMenuDao.findCaseMenuByRole" column="{menuId=menu_id}">
            </collection>
        </resultMap>
    
    
        <!--查询树级菜单-->
        <select id="findCaseMenuByRole" resultMap="BaseResultMap" >
            select distinct a.menu_id ,a.parent_id,a.menu_url,a.menu_name ,a.menu_type,a.menu_icon
            from hdhr_menu a inner join hdhr_role_menu b on a.menu_id = b.menu_id
            where a.is_enable = 1
            <choose>
                <when test="menuId != null ">
                    and a.parent_id = #{menuId}
                </when>
                <otherwise>
                    and a.parent_id is null
                </otherwise>
            </choose>
            order By a.sort asc
    </select>
    

    2.通过递归算法获取

    public class TreeStructure {
        /**
         * 循环父级菜单 ---调用父级菜单即可
         * @param menuList
         * @return
         */
        public static List<MenuRoleVO> loopFatherMenuWithRole(List<MenuRoleVO> menuList){
            List<MenuRoleVO> treeList = new ArrayList<>();
            for(MenuRoleVO menuVO:menuList){
                if(StringUtils.isBlank(menuVO.getParentId())){
                    treeList.add(loopChildrenMenuWithRole(menuList,menuVO));
                }
            }
            return treeList;
        }
        /**
         * 循环子集菜单
         * @param menuList
         * @param fatherMenuVO
         */
        public static MenuRoleVO loopChildrenMenuWithRole(List<MenuRoleVO> menuList,MenuRoleVO fatherMenuVO){
            String fatherId =  fatherMenuVO.getMenuId();
            for(MenuRoleVO childrenMenu:menuList){
                String childrenFatherId = childrenMenu.getParentId();
                if(StringUtils.equals(fatherId,childrenFatherId)){
                    fatherMenuVO.getMenuList().add(loopChildrenMenuWithRole(menuList,childrenMenu));
                }
            }
            return  fatherMenuVO;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:树形菜单

          本文链接:https://www.haomeiwen.com/subject/shyzmltx.html