【Java】多级菜单实现方式

这篇博客介绍了如何使用Java实现多级菜单的排序和递归查询。通过过滤、排序和集合操作,将数据库中的一级分类组装成树形结构。同时,展示了Mybatis中使用递归查询子菜单的方法,以及通过For循环和HashMap存储菜单的两种实现方式。这些方法适用于构建层次结构清晰的菜单系统。
摘要由CSDN通过智能技术生成

多级菜单实现方式

gitee代码

Java组装 递归

按照Sort进行排序

public List<CategoryEntity> list() {
    //1、查出所有分类
    List<CategoryEntity> entities = baseMapper.selectList(null);
    //2、组装成父子的树形结构
    //2.1)、找到所有的一级分类
    List<CategoryEntity> level1Menus = entities.stream()
        .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
        .peek((menu) -> menu.setChildren(getChildren(menu, entities)))
        .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
        .collect(Collectors.toList());
    return level1Menus;
}

private List<CategoryEntity> getChildren(CategoryEntity item, List<CategoryEntity> all) {
    List<CategoryEntity> children = all.stream()
        .filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), item.getCatId()))
        .peek(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildren(categoryEntity, all));
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
        .collect(Collectors.toList());
    return children;
}
使用Mybatis递归数据库
<mapper namespace="com.zxy.mp.mapper.CategoryMapper">
    <resultMap type="com.zxy.mp.domain.CategoryEntity" id="baseResultMap">
        <result property="catId" column="cat_id"/>
        <result property="name" column="name"/>
        <result property="parentCid" column="parent_cid"/>
        <result property="catLevel" column="cat_level"/>
        <result property="showStatus" column="show_status"/>
        <result property="sort" column="sort"/>
        <result property="icon" column="icon"/>
        <result property="productUnit" column="product_unit"/>
        <result property="productCount" column="product_count"/>
    </resultMap>
    <resultMap id="complexResultMap" type="com.zxy.mp.domain.CategoryEntityItem" extends="baseResultMap">
        <!--递归查询 list 设置子菜单-->
        <collection property="children" select="list" column="cat_id"/>
    </resultMap>
    <select id="list" resultType="com.zxy.mp.domain.CategoryEntity" resultMap="complexResultMap">
        select * from pms_category where parent_cid=#{id}
    </select>
</mapper>
使用For对象引用收集

只能适用于二级菜单

public List<CategoryEntity> list() {
    List<CategoryEntity> entities = baseMapper.selectList(null);
    List<CategoryEntity> list = new ArrayList<>();
    for (CategoryEntity entity : entities) {
        if(entity.getParentCid().equals(0L)){
            list.add(entity);
        }else{
            for (CategoryEntity parent : list) {
                if(entity.getParentCid().equals(parent.getCatId())){
                    parent.getChildren().add(entity);
                    // 在使用递归无限子节点查询存储 parent.setChildren(getChildren(parent,entities));
                }
            }
        }
    }
    return list;
}
使用HashMap存储菜单

(oldItem, newItem) -> newItem : 如果key重复选取新的value值

public List<CategoryEntity> list() {
    List<CategoryEntity> entities = baseMapper.selectList(null);
    // 封装成Map
    Map<Long, CategoryEntity> categoryMap = entities.stream().
            collect(Collectors.toMap(CategoryEntity::getCatId, item -> item, (oldItem, newItem) -> newItem));
    List<CategoryEntity> list = new ArrayList<>();
    for (CategoryEntity entity : entities) {
        if(entity.getParentCid().equals(0L)){
            list.add(entity);
        }else{
           // 由于Map中的菜单实体 是list中的 ,所以修改Map中的value就是修改List中的实体
           categoryMap.get(entity.getParentCid()).getChildren().add(entity);
        }
    }
    return list;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明明吃了饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值