根据数据库的自关联外键构建树形json数据
数据库设计
CREATE TABLE `sys_menu` (
`id` bigint NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL COMMENT '菜单名称',
`appUrl` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '程序路径',
`imgUrl` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图标路径',
`permission_code` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '权限码',
`sortCode` int NOT NULL COMMENT '排序码',
`parentId` bigint NOT NULL DEFAULT '0' COMMENT '上级菜单Id',
`modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb3 COMMENT='菜单实体';
根据里面的parentId来递归构建子菜单(一级菜单的parentId默认为0)
上Service代码
public Result menuTree() {
LambdaQueryWrapper<SysMenu> rootWrapper = new LambdaQueryWrapper<>();
rootWrapper.eq(SysMenu::getParentId, 0);
rootWrapper.orderByAsc(SysMenu::getSortCode);
List<SysMenu> firstMenu = baseMapper.selectList(rootWrapper);
List<TreeNode> result = new ArrayList<>(firstMenu.size());
for (SysMenu menu : firstMenu) {
TreeNode treeNode = new TreeNode();
treeNode.setId(menu.getId());
treeNode.setName(menu.getName());
treeNode.setAppUrl(menu.getAppUrl());
treeNode.setImgUrl(menu.getImgUrl());
treeNode.setPermissionCode(menu.getPermissionCode());
treeNode.setParentId(menu.getParentId());
treeNode.setCreateTime(menu.getCreateTime());
result.add(treeNode);
}
for (TreeNode node : result) {
// 关键就是这段代码,给每个一级菜单赋子节点
node.setChildren(this.getChildren(node.getId()));
}
return Result.ok().data(result);
}
1、这里通过parentId=0筛选出一级菜单
2、构建出TreeNode一级节点(TreeNode代码在下面)
3、遍历一级菜单为子节点赋值
TreeNode实体
public class TreeNode {
private Long id;
private String name;
private String appUrl;
private String imgUrl;
private String permissionCode;
private Long parentId;
private Date createTime;
private List<TreeNode> children;
}
getChildren方法:
public List<TreeNode> getChildren(Long fatherId) {
LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysMenu::getParentId, fatherId);
queryWrapper.orderByAsc(SysMenu::getSortCode);
// 通过父节点id查到所有的子节点,并按照表中的sortCode排序
List<SysMenu> childrenMenu = baseMapper.selectList(queryWrapper);
// 没有子节点则退出
if (childrenMenu.size() < 1) {
return null;
}
// 添加子节点
List<TreeNode> treeNodes = new ArrayList<>(childrenMenu.size());
for (SysMenu menu : childrenMenu) {
TreeNode treeNode = new TreeNode();
treeNode.setId(menu.getId());
treeNode.setName(menu.getName());
treeNode.setAppUrl(menu.getAppUrl());
treeNode.setImgUrl(menu.getImgUrl());
treeNode.setPermissionCode(menu.getPermissionCode());
treeNode.setParentId(menu.getParentId());
treeNode.setCreateTime(menu.getCreateTime());
treeNodes.add(treeNode);
}
for (TreeNode node : treeNodes) {
if (node.getId() != null) {
// 递归查询子节点下是否还有节点
node.setChildren(getChildren(node.getId()));
}
}
return treeNodes;
}
到这里一颗对象树就构建完成了通过序列化返回给前端之后就是一颗json树了
感谢观看!!!!