哈喽!大家好,今天我们来看看树形结构的递归取数,说实话,这个呢,我其实也不太明白其中的原理,也就是我也不会灵活运用,之所以写下来,嘿嘿,是为了希望那行和我一样不会这个的,或者不懂这个递归的,可以有所帮助,然后,也方便那天我自己忘记了,查找起来也省力
一、测试Demo版
实体类
@Data
public class Dept implements Serializable {
private int id;
private int parentId;
private String name;
private List child = new ArrayList();
public Dept(int id, int parentId, String name) {
super();
this.id = id;
this.parentId = parentId;
this.name = name;
}
}
测试Test
public classTestDemo {private static ListdeptList;static{
Dept dept1= new Dept(1, 0, "中国");
Dept dept2= new Dept(2, 1, "北京");
Dept dept3= new Dept(3, 1, "上海");
Dept dept4= new Dept(4, 1, "广东");
Dept dept5= new Dept(5, 4, "广州");
Dept dept6= new Dept(6, 4, "深圳");
Dept dept7= new Dept(7, 5, "天河区");
deptList= new ArrayList();
deptList.add(dept1);
deptList.add(dept2);
deptList.add(dept3);
deptList.add(dept4);
deptList.add(dept5);
deptList.add(dept6);
deptList.add(dept7);
}private static List buildTree(List deptList, intpid) {
List treeList = new ArrayList();for(Dept dept : deptList) {if (dept.getParentId() ==pid) {
dept.setChild(buildTree(deptList, dept.getId()));
treeList.add(dept);
}
}returntreeList;
}public static voidmain(String[] args) {
Map map = new HashMap();
map.put("data", buildTree(deptList, 0));
System.out.println(JSON.toJSONString(map));
}
}
二、业务类型
1.实体类
@Datapublic classPerMenuBO extends BaseVo {
@ApiModelProperty(value= "自增主键ID")privateLong id;
@ApiModelProperty(value= "资源名称")privateString name;
@ApiModelProperty(value= "菜单权限标识")privateString authorityTag;
@ApiModelProperty(value= "前端跳转URL")privateString path;
@ApiModelProperty(value= "后端url")privateString requestPath;
@ApiModelProperty(value= "菜单组件")privateString componet;
@ApiModelProperty(value= "父菜单ID")privateLong parentId;
@ApiModelProperty(value= "图标")privateString icon;
@ApiModelProperty(value= "排序(默认1)")privateInteger sort;
@ApiModelProperty(value= "是否管控频次")privateBoolean controlFrequency;
@ApiModelProperty(value= "频次")privateInteger frequency;
@ApiModelProperty(value= "资源类型 (0:目录,1:菜单,2:按钮)")privateInteger type;
@ApiModelProperty(value= "逻辑删除标记(0:正常, 1:删除)")privateBoolean delFlag;private List child = new ArrayList();
}
2.mapper类
public interfacePerMenuMapper {
ListgetList(PerMenu model);
}
3.service类
public interfaceIPerMenuService {
ListgetListTree(PerMenuVO model);
}
4.serviceImpl类
@Servicepublic classPerMenuService extends AbstractBaseService implements IPerMenuService {
@AutowiredprivatePerMenuMapper perMenuMapper;
@Overridepublic ListgetListTree(PerMenuVO model) {
PerMenu param= copyProperties(model, PerMenu.class);
List list =perMenuMapper.getList(param);
List tree = buildTree(list, 0L);returntree;
}public List buildTree(Listlist, Long pid) {
List listM = new ArrayList();for(PerMenuBO menuBO : list) {if(menuBO.getParentId().equals(pid)) {
menuBO.setChild(buildTree(list, menuBO.getId()));
listM.add(menuBO);
}
}returnlistM;
}
}
5.Controller类
@RestControllerpublic classPerMenuController {
@AutowiredprivateIPerMenuService perMenuService;@PostMapping("/perMenu/getlisttree")
@AutoLog(description= "递归查询资源菜单列表接口")
@ApiOperation(value= "递归查询资源菜单列表接口")public BaseResponse>getListTree(@RequestBody PerMenuVO request) {return new BaseResponse(true, "查询成功", 0, perMenuService.getListTree(request));
}
}