java後端遞迴迴圈取數(樹形結構)
阿新 • • 發佈:2021-01-19
哈嘍!大家好,今天我們來看看樹形結構的遞迴取數,說實話,這個呢,我其實也不太明白其中的原理,也就是我也不會靈活運用,之所以寫下來,嘿嘿,是為了希望那行和我一樣不會這個的,或者不懂這個遞迴的,可以有所幫助,然後,也方便那天我自己忘記了,查詢起來也省力
一、測試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)); } }