Spring boot 项目(三)—— mybatis-plus构建菜单树
程序员文章站
2022-04-25 07:59:47
...
controller
@RequestMapping("menuTree")
public Object menuTree(){
return service.listSysMenuWithTree();
}
service
public interface SysMenuService extends IService<SysMenu>
Object listSysMenuWithTree();
serviceImpl
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService
@Override
public Object listSysMenuWithTree() {
// 获取所有菜单
List<SysMenu> sysMenuList = list(new LambdaQueryWrapper<SysMenu>()
.eq(SysMenu::getMenuType, GlobalConstant.MENU_TYPE_ME)
);
// 一级菜单 => jdk8 流操作
List<SysMenu> firstMenuList = sysMenuList.stream()
// 过滤出一级菜单
.filter(s -> s.getMenuParentId().equals(GlobalConstant.MENU_TOP))
// 返回list集合形式
.collect(Collectors.toList());
// 遍历一级菜单 构造菜单树
for (SysMenu sysMenu : firstMenuList) {
getChildren(sysMenu, sysMenuList);
}
if (firstMenuList.size() != 0) {
return ResultJson.ok("查询成功", firstMenuList);
} else {
return ResultJson.error("查询失败");
}
}
/**
* 获取子菜单
*/
private SysMenu getChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {
for (SysMenu menu : sysMenuList) {
if (menu.getMenuParentId().equals(sysMenu.getId())) {
sysMenu.getChildrenList().add(getChildren(menu, sysMenuList));
}
}
return sysMenu;
}
entity
private Long id;
//资源名称
private String menuName;
//权限
private String menuPermission;
//对应url
private String menuPath;
//父节点id
private Long menuParentId;
//菜单类型0菜单,1栏目类型
private Integer menuType;
//排序字段:值越小 顺序越靠前
private Integer orderNum;
//创建时间
private LocalDateTime createTime;
//最后更新时间
private LocalDateTime updateTime;
//子菜单集合
@TableField(exist = false)
private Set<SysMenu> childrenList=new HashSet<>();
上一篇: mysql添加联合主键