保 洁 阿 姨

拥有60+年的前端设计经验

60+Years of front-end design experience

luzhou·sichuan

www.usuuu.com

Web网页设计:70888820

2个视频(Video)

首页/破碎代码/Spring Boot + Mybatis Plus实现树状菜单的方法/

Spring Boot + Mybatis Plus实现树状菜单的方法

发布于:2022-11-27 13:02:14/浏览量:231/IP属地:四川省

controller

/**
  * 菜单列表
  */
@GetMapping("/list")
public R<?> lists() {
     return R.ok(menuService.lists());
}

service

/**
  * 查询全部菜单
  */
List<MenuPo> lists();

ServiceImpl

/**
  * 菜单列表
  */
@Override
@Cacheable(cacheNames = "menu")
public List<MenuPo> lists() {
    LambdaQueryWrapper<MenuPo> menuPoWrapper = Wrappers.lambdaQuery();
    menuPoWrapper.orderByAsc(MenuPo::getSort);
    List<MenuPo> list = menuMapper.selectList(menuPoWrapper);
    return dataToTreeList(list);
}
/**
 * 数据转树形结构
 */
private List<MenuPo> dataToTreeList(List<MenuPo> allList) {
    return allList.parallelStream()
        //父级ID为0
         .filter(item -> "0".equals(item.getPid()))
         .map(menu -> menu.setChildren(getTreeChilds(allList, menu.getId())))
         .collect(Collectors.toList());
}
/**
 * 递归子级
 */
private List<MenuPo> getTreeChilds(List<MenuPo> allList, String pid) {
    return allList.parallelStream()
        .filter(item -> pid.equals(item.getPid()))
        .map(menu -> menu.setChildren(getTreeChilds(allList, menu.getId())))
        .collect(Collectors.toList());
}

返回结果

{
	"status": 200,
	"msg": "success",
	"data": [
		{
			"id": "1590219111046717441",
			"pid": "0",
			"name": "一级菜单",
			"type": 1,
			"icon": "",
			"sort": 1,
			"status": true,
			"template": "",
			"description": "简介",
			"keywords": "关键字",
			"createTime": "2022-11-09 13:45:56",
			"updateTime": "2022-11-27 12:51:57",
			"children": [
				{
					"id": "1596728570011074562",
					"pid": "1590219111046717441",
					"name": "二级菜单",
					"type": 2,
					"icon": "",
					"sort": 2,
					"status": true,
					"template": "",
					"description": "简介",
					"keywords": "关键字",
					"createTime": "2022-11-27 12:52:12",
					"updateTime": null,
					"children": []
				}
			]
		}
	]
}
© 未经允许禁止转载
回复
暂无数据

暂无数据

签到奖励积分

站点公告

  • Web网页设计交流群:70888820
  • QQ号为523179414的狗,谢绝访问

版权申明

本站原创内容版权遵循 CC-BY-NC-SA 协议规定,转载请注明出处.本站部分资源收集于网络,只做学习和交流使用,版权归原作者所有.