菜单递归查询
Posted yqj234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菜单递归查询相关的知识,希望对你有一定的参考价值。
springboot 实现递归查询菜单 - 渺小的人类 - 博客园
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing :
springboot 实现递归查询菜单 - 渺小的人类 - 博客园
package com.java1234.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
* 菜单实体
*
* @author java1234
*
*/
@Entity
@Table(name = "t_menu")
public class Menu
@Id
@GeneratedValue
private Integer id; // 编号
@Column(length = 50)
private String name; // 菜单名称
@Column(length = 200)
private String url; // 菜单地址
private Integer state; // 菜单节点类型 1 根节点 0 叶子节点
@Column(length = 100)
private String icon; // 图标
private Integer pId; // 父菜单Id
@Transient
private List<Menu> treeList = new ArrayList<>();
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getUrl()
return url;
public void setUrl(String url)
this.url = url;
public Integer getState()
return state;
public void setState(Integer state)
this.state = state;
public String getIcon()
return icon;
public void setIcon(String icon)
this.icon = icon;
public Integer getpId()
return pId;
public void setpId(Integer pId)
this.pId = pId;
@Override
public String toString()
return "[id=" + id + ", name=" + name + ", url=" + url + ", state=" + state + ", icon=" + icon + ", pId=" + pId
+ "]";
/**
* @return the treeList
*/
public List<Menu> getTreeList()
return treeList;
/**
* @param treeList the treeList to set
*/
public void setTreeList(List<Menu> treeList)
this.treeList = treeList;
// -------------------------------递归算法-----------------------------------
/**
* 获取所有权限
*
* @return
*/
public List<Menu> getMenuList()
// 用boot获取分类数据 selectAll在springboot中是获取数据表里的所有数据
List<Menu> data = menuService.list();
// 定义新的list
List<Menu> menuList = new ArrayList<>();
// 先找到所有的一级分类
for (Menu menuInfo : data)
// 一级菜单的parentId是0
if (menuInfo.getpId() == -1)
menuList.add(menuInfo);
// 为一级菜单设置子菜单,getChild是递归调用的
for (Menu parentMenuInfo : menuList)
parentMenuInfo.setTreeList(getChilde(parentMenuInfo.getId(), data));
return menuList;
/**
* 递归查找子菜单
*
* @param id 当前菜单id
* @param rootList 要查找的列表
* @return
*/
private List<Menu> getChilde(Integer id, List<Menu> rootList)
// 子级
List<Menu> childList = new ArrayList<>();
for (Menu menuInfo : rootList)
// 遍历所有节点,将父级id与传过来的id比较
if (menuInfo.getpId().equals(id))
childList.add(menuInfo);
// 把子级的子级再循环一遍
for (Menu sonMenuInfo : childList)
sonMenuInfo.setTreeList(getChilde(sonMenuInfo.getId(), rootList));
// 递归退出条件
if (childList.size() == 0)
return null;
return childList;
@RequestMapping("/tset")
@ResponseBody
public List<Menu> test()
List<Menu> menuList = this.getMenuList();
return menuList;
以上是关于菜单递归查询的主要内容,如果未能解决你的问题,请参考以下文章