你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者没有积分想获取项目和定制项目,欢迎指教: 企鹅:869192208
实体类
@Data
@ApiModel("功能菜单实体类")
public class MenuModel{
private Integer id;
private String title;
private String icon;
private String href;
private String target;
private Integer parentId;
private Integer type;
private List<MenuModel> child;
}
service实现
public List<MenuModel> query(Integer userId) {
List<MenuModel> menus = menuDao.query(userId);
List<MenuModel> parentMenus = menus.stream().filter(l -> l.getParentId().equals(0)).map(m -> {
m.setChild(getChildrens(m, menus));
return m;
}).collect(Collectors.toList());
return parentMenus;
}
private List<MenuModel> getChildrens(MenuModel root, List<MenuModel> all) {
List<MenuModel> children = all.stream().filter(m -> {
return Objects.equals(m.getParentId(), root.getId());
}).map(
m -> {
m.setChild(getChildrens(m, all));
return m;
}
).collect(Collectors.toList());
return children;
}
|