多级菜单实现方式
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;
}