java树形菜单实现 JPA实现树形菜单

基于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);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章