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