分组的实体TDataGroup类就不写了,强调一点要有parentId,加上一个sort最好
controller-----------------------------------------------------------------------------------------------
@RequestMapping(value = “/tree.json”, method = RequestMethod.POST)
@ApiOperation(value = “获取分组树形结构”, notes = “获取分组树形结构”)
public JsonResult<List> tree() {
List tree = tDataGroupService.getGroupTree();
return JsonResult.success(tree);
}
service-----------------------------------------------------------------------------------------------
public List getGroupTree() {
List list = tDataGroupDao.all();
TGroupItem root = GroupBuildUtil.buildOrgTree(list, 0L);
return buildGroupTree(root);
}
private List<GroupNodeView> buildGroupTree(TGroupItem node) {
List<TGroupItem> list = node.getChildren();
if (list.size() == 0) {
return Collections.EMPTY_LIST;
}
List<GroupNodeView> views = new ArrayList<GroupNodeView>(list.size());
for (TGroupItem item : list) {
GroupNodeView view = new GroupNodeView();
view.setGroupName(item.getData().getGroupName());
view.setGroupDesc(item.getData().getGroupDesc());
view.setSort(Long.valueOf(item.getData().getSort()));
view.setModifyUser(Integer.valueOf(item.getData().getModifyUser().toString()));
view.setId(item.getData().getId());
List<GroupNodeView> children = this.buildGroupTree(item);
if (CollectionUtils.isNotEmpty(children)) {
Map<String, Object> addProperties = new HashMap() {{
put("children", children);
}};
GroupNodeView viewTmp = (GroupNodeView) ReflectUtil.getTarget(view, addProperties);
views.add(viewTmp);
} else {
views.add(view);
}
}
return views;
}
节点视图实体类-----------------------------------------------------------------------------------------------
public class GroupNodeView {
private String groupName;
private String groupDesc;
private Long sort;
private Integer modifyUser;
private Long id;
// List children=new ArrayList();
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
public Long getSort() {
return sort;
}
public void setSort(Long sort) {
this.sort = sort;
}
public Integer getModifyUser() {
return modifyUser;
}
public void setModifyUser(Integer modifyUser) {
this.modifyUser = modifyUser;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// public List getChildren() {
// return children;
// }
// public void setChildren(List children) {
// this.children = children;
// }
@Override
public String toString() {
return “GroupNodeView [groupName=” + groupName + “, groupDesc=” + groupDesc + “, sort=” + sort + “, modifyUser=”
+ modifyUser + “, id=” + id + “, children=]”;
}
}
TGroupItem 类-----------------------------------------------------------------------------------------------
public class TGroupItem implements TreeItem{
TDataGroup sysGroup = null;
List<TGroupItem> children = new ArrayList<>();
@JsonIgnore
TGroupItem parent = null;
String groupDesc ;
public TGroupItem(TDataGroup sysGroup){
this.sysGroup = sysGroup;
this.groupDesc = sysGroup!=null?sysGroup.getGroupDesc():null;
}
public void setParent(TGroupItem parent){
this.parent = parent;
parent.children.add(this);
}
public List<TGroupItem> getChildren(){
return this.children;
}
public Long getId(){
return sysGroup.getId();
}
public TDataGroup getData(){
return this.sysGroup;
}
/**
* 查找某个指定的子节点
* @param functionId
* @return
*/
public TGroupItem findChild(long functionId){
if(sysGroup.getId()==functionId){
return this;
}
for(TGroupItem item:children){
TGroupItem find = item.findChild(functionId);
if(find!=null){
return find;
}
}
return null;
}
/**
* 查找所有的子节点
* @return
*/
public List<TGroupItem> findAllItem(){
List<TGroupItem> all = new LinkedList<>();
findAllChildItem(all,this);
return all;
}
public List<Long> findAllChildrenId(){
List<TGroupItem> items =findAllItem();
List<Long> children = new ArrayList<Long>();
for(TGroupItem item:items){
children.add(item.getId());
}
return children;
}
private void findAllChildItem(List<TGroupItem> all,TGroupItem parent){
for(TGroupItem item:parent.children){
all.add(item);
findAllChildItem(all,item);
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sysGroup == null) ? 0 : sysGroup.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TGroupItem other = (TGroupItem) obj;
if (sysGroup == null) {
if (other.sysGroup != null)
return false;
} else if (!sysGroup.equals(other.sysGroup))
return false;
return true;
}
@Override
public String toString() {
return "MenuItem [sysGroup=" + sysGroup.getGroupName() + ","+this.children.size()+"]";
}
@Override
public String getName() {
return sysGroup==null?"":sysGroup.getGroupName();
}
}
接口TreeItem -----------------------------------------------------------------------------------------------
public interface TreeItem extends java.io.Serializable {
public String getName();
public Long getId();
public List getChildren();
}