//获取全部数据 List<Menu> menus = iMenuService.selectMenuList(userId,roleId); //根据parentId去分组 Map<Long, List<Menu>> collect = menus.stream().collect(Collectors.groupingBy(Menu::getParentId)); //把你分组的数据 根据某个属性去set到父集合中,这个children集合是建立在Menu这个实体类中的,这些代码的基础也是源于Menu这个实体类 for (Menu menu : menus) { menu.setChildren(collect.get(menu.getMenuId())); } //过滤父集合数据,也就是父级菜单,我这里父级菜单parentid是0,所以根据这个去判断 List<Menu> newMenus = menus.parallelStream().filter(i -> i.getParentId() == 0).collect(Collectors.toList());最终封装的数据:
{ "menuId": 1, "roleId": null, "menuName": "系统管理", "parentId": 0, "parentPath": "/system", "orderNum": "2", "menuUrl": "/system", "component": null, "isFrame": null, "redirect": "test", "menuType": "M", "hidden": "1", "perms": null, "icon": "icon-settings", "routeName": "system", "cacheable": "0", "affix": "0", "children": [ { "menuId": 100, "roleId": null, "menuName": "用户管理", "parentId": 1, "parentPath": "/system", "orderNum": "2", "menuUrl": "/system/user", "component": "system/user/index", "isFrame": null, "redirect": "test.com", "menuType": "C", "hidden": "1", "perms": null, "icon": "IconMenu", "routeName": "user", "cacheable": "0", "affix": "0", "children": null, "createTime": null }, { "menuId": 101, "roleId": null, "menuName": "角色管理", "parentId": 1, "parentPath": "/system", "orderNum": "3", "menuUrl": "/system/role", "component": "system/role/index", "isFrame": null, "redirect": "test", "menuType": "C", "hidden": "1", "perms": null, "icon": "IconMenu", "routeName": "role", "cacheable": "0", "affix": "0", "children": null, "createTime": null }, { "menuId": 102, "roleId": null, "menuName": "菜单管理", "parentId": 1, "parentPath": "/system", "orderNum": "4", "menuUrl": "/system/menu", "component": "system/menu/index", "isFrame": null, "redirect": "test.com", "menuType": "C", "hidden": "1", "perms": null, "icon": "IconMenu", "routeName": "menu", "cacheable": "0", "affix": "0", "children": null, "createTime": null }, { "menuId": 103, "roleId": null, "menuName": "部门管理", "parentId": 1, "parentPath": "/system", "orderNum": "1", "menuUrl": "/system/department", "component": "system/dept/index", "isFrame": null, "redirect": "test4.com", "menuType": "C", "hidden": "1", "perms": null, "icon": "IconMenu", "routeName": "department", "cacheable": "0", "affix": "0", "children": null, "createTime": null } ], "createTime": null }
实体类代码
@JsonProperty注解主要用于实体类的属性上,作用可以简单的理解为在反序列化的时候给属性重命名(多一个名字来识别),我的使用是因为,在返回数据的时候把字段的is字样消除,通过注解给重命名;
@JsonInclude(value= JsonInclude.Include.NON_NULL)注解用于返回前端的实体类中如果某个字段为空的话那么就不返回这个字段了
个人建议boolean字段及含有判断的字段尽量避免is_开头,因为部分框架解析会引起序列化错误,因此可能造成代码的异常,详情可浏览阿里巴巴开发手册
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; /** * @Project: test * @Package: * @Author: pangyq * @CreateTime: 2022-08-03 14:28 * @Description: TODO * @Version: 1.0 */ @Data @JsonInclude(value= JsonInclude.Include.NON_NULL) public class Menu extends Model<Menu> { private static final long serialVersionUID = 1L; /** * 菜单ID */ @TableId(type = IdType.INPUT) private Long menuId; /** * 角色ID */ private Long roleId; /** * 菜单名称 */ private String menuName; /** * 父菜单ID */ private Long parentId; /** * 上级菜单 */ private String parentPath; /** * 显示顺序 */ private String orderNum; /** * 路由地址 */ private String menuUrl; /** * 组件路径 */ private String component; /** * 是否为外链(0是 1否) */ private String isFrame; /** * 外链地址 */ private String redirect; /** * 类型(M目录 C菜单 F按钮) */ private String menuType; /** * 菜单状态:0显示,1隐藏 */ private String hidden; /** * 权限字符串 */ private String perms; /** * 菜单图标 */ private String icon; /** * 路由名称 */ private String routeName; /** * 是否缓存(0是 1否) */ private String cacheable; /** * 是否固定标题栏(0是 1否) */ private String affix; /** * 子菜单 */ @JsonInclude(value= JsonInclude.Include.NON_NULL) private List<Menu> children = new ArrayList<Menu>(); /** * 创建时间 */ private LocalDateTime createTime; private String badge; @JsonProperty(value = "isRootPath") private boolean isRootPath; @JsonProperty(value = "isSingle") private boolean isSingle; private String localFilePath; }
集合按照某个属性去分组,适用于获取树形菜单等场景
于 2022-08-05 13:05:14 首次发布