基於springboot + JPA
方法一:
樹結構:
@Setter
@Getter
public class TreeNode<T> {
@ApiModelProperty(value = "id", notes = "id")
public Long id;
@ApiModelProperty(value = "父ID", notes = "父ID")
private Long parentId;
@ApiModelProperty(value = "子節點", notes = "子節點")
private List<T> children = new ArrayList<>();
}
部門:
@Entity
@Table(name = "department")
@Getter
@Setter
public class Department {
public static final int STATUS_DISABLED = 0;
public static final int STATUS_ENABLED = 1;
@Column(nullable = false, columnDefinition="bigint COMMENT '唯一標識'")
@Id
private Long id;
@Column(nullable = false, columnDefinition = "varchar(255) COMMENT '部門名稱'")
private String departmentName;
@Column(nullable = false, columnDefinition = "varchar(1000) COMMENT '部門描述'")
private String departmentDesc;
@Column(nullable = false, columnDefinition = "tinyint(1) DEFAULT 1 COMMENT '狀態(1:可用,0:禁用)'")
private Integer status;
@Column(nullable = false, columnDefinition = "bigint(20) COMMENT '歸屬人ID'")
private Long userId;
@Column(columnDefinition = "bigint(20) COMMENT '父ID'")
private Long parentId;
}
部門VO:
@Getter
@Setter
@ApiModel(value = "部門VO")
public class DepartmentVO extends TreeNode<DepartmentVO> implements Serializable {
@ApiModelProperty(value = "部門名稱", notes = "部門名稱")
public String departmentName;
@ApiModelProperty(value = "部門描述", notes = "部門描述")
private String departmentDesc;
}
方法實現
private List<DepartmentVO> getTree(List<DepartmentVO> list) {
Map<Long, DepartmentVO> dtoMap = new HashMap<>();
for (DepartmentVO node : list) {
dtoMap.put(node.getId(), node);
}
List<DepartmentVO> resultList = new ArrayList<>();
for (Map.Entry<Long, DepartmentVO> entry : dtoMap.entrySet()) {
DepartmentVO node = entry.getValue();
if (node.getParentId() == null) {
// 如果是頂層節點,直接添加到結果集合中
resultList.add(node);
} else {
// 如果不是頂層節點,找其父節點,並且添加到父節點的子節點集合中
if (dtoMap.get(Long.valueOf(node.getParentId())) != null) {
dtoMap.get(Long.valueOf(node.getParentId())).getChildren().add(node);
}
}
}
return resultList;
}
調用
List<Department> list = departmentRepository.findByUserIdAndStatus(userVO.getParentId(), Department.STATUS_ENABLED);
//重寫工具類實現,就是把數組從 對象-> 對象VO 數組
List<DepartmentVO> result = BeanUtils.copyProperties(list, DepartmentVO.class);
result = getTree(result);
方法二:
對數據庫壓力有點大,查詢多次,建議用方法一
因先使用方法二實現,後改用方法一,所以方法二調用需要自己測試。
部門實體
@Entity
@Table(name = "epartment")
@Getter
@Setter
public class Department{
public static final int STATUS_DISABLED = 0;
public static final int STATUS_ENABLED = 1;
@Column(nullable = false, columnDefinition="bigint COMMENT '唯一標識'")
@Id
private Long id;
@Column(nullable = false, columnDefinition = "varchar(255) COMMENT '部門名稱'")
private String departmentName;
@Column(nullable = false, columnDefinition = "varchar(1000) COMMENT '部門描述'")
private String departmentDesc;
@Column(nullable = false, columnDefinition = "tinyint(1) DEFAULT 1 COMMENT '狀態(1:可用,0:禁用)'")
private Integer status;
@Column(nullable = false, columnDefinition = "bigint(20) COMMENT '歸屬人ID'")
private Long userId;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "PARENT_ID", columnDefinition = "bigint(20) COMMENT '父ID'")
private Department parent;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<Department> children = new ArrayList<>();
}
調用方法
//通過某個用戶拿出該用戶所有主部門數據,就會帶出來子節點
//最好初始化一個主節點ID爲0
Department department = new Department();
department.setId(0L);
List<Department> list = departmentRepository.findByUserIdAndParent(userId,department);