把這種的遞歸,參數是PID
@RequestMapping(value="/queryListMenu/{pid}",method=RequestMethod.GET)
public List<Menu> queryMenu(@PathVariable(value = "pid",required = true) String pid) {
List<Menu> list = new ArrayList<>();
List<Menu> listMenuParent = new ArrayList<>();
Boolean flag = true;
while (flag) {
//menuParent中,沒有List<Menu>child 爲 null
Menu menuParent = orgService.findByPid(pid);
//如果最後一條數據爲null則把之前的數據賦給list並返回作爲最後輸出
list = listMenuParent;
//如果最後一條數據爲null則flag爲false,跳出if判斷
flag = false;
//這裏pid 是作爲child的id的
Menu menuChild = orgService.findById(pid);
if (menuChild != null && menuParent != null) {
List<Menu> listChild = new ArrayList<>(1);
/*String s = JSONObject.toJSONString(menuChild);
Object parse = JSON.parse(s);*/
listChild.add(menuChild);
//一個完整的實體類對象,有List<Menu>child
menuParent.setChild(listChild);
//一個一個menu放入集合
//listMenuParent.add(menuParent);
listMenuParent.add(menuParent);
if (StringUtils.isNotBlank(menuChild.getPid())){
pid = menuChild.getPid();
//當爲true時又返回if判斷
flag = true;
}else {
//如果最後一條PID爲空則直接返回之前的數據
return listMenuParent;
}
}
}
return list;
}
@Entity
@Table(name = "sys_menu")
public class Menu implements Serializable {
@Id
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
@Column(name = "PID")
private String pid;
@Transient
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Menu> child;
輸出:
[
{
"id": "1",
"name": "LJ",
"pid": "2",
"child": [
{
"id": "2",
"name": "ZF",
"pid": "3",
"child": [
{
"id": "3",
"name": "SP",
"pid": "4",
"child": [
{
"id": "4",
"name": "WS",
"pid": "5",
"child": [
{
"id": "5",
"name": "HXM",
"pid": "6",
"child": [
{
"id": "6",
"name": "GMY",
"pid": "7",
"child": null
}
]
}
]
}
]
}
]
}
]
},
{
"id": "2",
"name": "ZF",
"pid": "3",
"child": [
{
"id": "3",
"name": "SP",
"pid": "4",
"child": [
{
"id": "4",
"name": "WS",
"pid": "5",
"child": [
{
"id": "5",
"name": "HXM",
"pid": "6",
"child": [
{
"id": "6",
"name": "GMY",
"pid": "7",
"child": null
}
]
}
]
}
]
}
]
},
{
"id": "3",
"name": "SP",
"pid": "4",
"child": [
{
"id": "4",
"name": "WS",
"pid": "5",
"child": [
{
"id": "5",
"name": "HXM",
"pid": "6",
"child": [
{
"id": "6",
"name": "GMY",
"pid": "7",
"child": null
}
]
}
]
}
]
},
{
"id": "4",
"name": "WS",
"pid": "5",
"child": [
{
"id": "5",
"name": "HXM",
"pid": "6",
"child": [
{
"id": "6",
"name": "GMY",
"pid": "7",
"child": null
}
]
}
]
},
{
"id": "5",
"name": "HXM",
"pid": "6",
"child": [
{
"id": "6",
"name": "GMY",
"pid": "7",
"child": null
}
]
}
]
總結:不同位置(i=0,i=1等)不同引用,但是相同對象的。如果對這個對象set,那麼在不同位置都可以看到set的值
原本想得到的數據是如下這種但是發現根本不行:
[
{
"id": "1",
"name": "LJ",
"pid": "2",
"child": [
{
"id": "2",
"name": "ZF",
"pid": "3",
"child": null
}]
},
{
"id": "2",
"name": "LJ",
"pid": "3",
"child": [
{
"id": "3",
"name": "ZF",
"pid": "4",
"child": null
}]
},
{
"id": "4",
"name": "LJ",
"pid": "5",
"child": [
{
"id": "5",
"name": "ZF",
"pid": "6",
"child": null
}]
}
]
想實現上述這種格式只能:
泛型改爲Object
private List<Object> child;
@Entity
@Table(name = "sys_menu")
public class Menu implements Serializable {
@Id
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
@Column(name = "PID")
private String pid;
@Transient
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Object> child;
@RequestMapping(value="/queryListMenu/{pid}",method=RequestMethod.GET)
public List<Menu> queryMenu(@PathVariable(value = "pid",required = true) String pid) {
List<Menu> list = new ArrayList<>();
List<Menu> listMenuParent = new ArrayList<>();
Boolean flag = true;
while (flag) {
//menuParent中,沒有List<Menu>child 爲 null
Menu menuParent = orgService.findByPid(pid);
//如果最後一條數據爲null則把之前的數據賦給list並返回作爲最後輸出
list = listMenuParent;
//如果最後一條數據爲null則flag爲false,跳出if判斷
flag = false;
//這裏pid 是作爲child的id的
Menu menuChild = orgService.findById(pid);
if (menuChild != null && menuParent != null) {
List<Object> listChild = new ArrayList<>();
//object對象中去掉爲null的child
String jsonMenu = JSONObject.toJSONString(menuChild);
Object parse = JSON.parse(jsonMenu);
listChild.add(parse);
//一個完整的實體類對象,有List<Menu>child
menuParent.setChild(listChild);
//一個一個menu放入集合
listMenuParent.add(menuParent);
if (StringUtils.isNotBlank(menuChild.getPid())){
pid = menuChild.getPid();
//當爲true時又返回if判斷
flag = true;
}else {
//如果最後一條PID爲空則直接返回之前的數據
return listMenuParent;
}
}
}
return list;
}
注意這段代碼(看註釋理解):
//object對象中去掉爲null的child,目的是下一次在走到這個位置set的值不在上一次看到
String jsonMenu = JSONObject.toJSONString(menuChild);
Object parse = JSON.parse(jsonMenu);