保 洁 阿 姨

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

60+Years of front-end design experience

luzhou·sichuan

www.usuuu.com

Web网页设计:70888820

4个视频

Video list

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

IP属地:四川省 / 发布于:4个月前 / 浏览:299

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": []
				}
			]
		}
	]
}
给Ta打赏
共0人打赏

打赏的土豪们

Rewarded users

暂无打赏

© 未经允许禁止转载
回复
暂无数据

暂无数据

Vexip UI

Ui components

VexipUI提供了一系列开箱即用的组件,并使用全新的Vue3.0组合式Api编写,一款值得关注的Vue3UI组件库

Vexip UI

用户签到

User Sign

签到奖励积分

站点公告

Announcement

(友情链接)2023-3-21清除单项链接
Web网页设计交流群:70888820
QQ号为523179414的狗,谢绝访问

版权申明

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