The implementation method of converting JAVA background into tree structure data and returning it to the front end

We often use tree structure, so is the data of tree structure done in the front end or in the background?

I have used the front-end ztree,selectTree and other components that belong to the front-end. The back-end only needs to return all the data of a table to the front-end. The front-end can divide the hierarchy by ID and PID. If we need the front-end to return the tree structure data directly from the back-end, then I will introduce the example I wrote to you.

Let's take a look at a picture to understand the tree structure. I'll look for any picture here to understand it.

Next let's look at the data, mainly including id,pid, name

Next, let's write a small example to convert recursively to numbers


package cn.ccccltd.ccccoi.mediumterminvestment.domain;
import java.util.List;
* @Description:Back to front end, business module
* @author yujiaqi
* @date: 2019 September 9, 2009 2:07:19 PM
public class ProjectBasicInfoDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String pId;
private String key;
private String title;
private String value;
private List<ProjectBasicInfoDTO> children;
private Boolean selectable;
private Boolean disableCheckbox;
private Boolean disabled;
public String getId() {
return id;
public String getValue() {
return value;
public void setValue(String value) {
this.value = value;
public void setId(String id) { = id;
public String getpId() {
return pId;
public void setpId(String pId) {
this.pId = pId;
public String getKey() {
return key;
public void setKey(String key) {
this.key = key;
public String getTitle() {
return title;
public void setTitle(String title) {
this.title = title;
public List<ProjectBasicInfoDTO> getChildren() {
return children;
public void setChildren(List<ProjectBasicInfoDTO> children) {
this.children = children;
public Boolean getSelectable() {
return selectable;
public void setSelectable(Boolean selectable) {
this.selectable = selectable;
public Boolean getDisableCheckbox() {
return disableCheckbox;
public void setDisableCheckbox(Boolean disableCheckbox) {
this.disableCheckbox = disableCheckbox;
public Boolean getDisabled() {
return disabled;
public void setDisabled(Boolean disabled) {
this.disabled = disabled;
public static long getSerialversionuid() {
return serialVersionUID;

service implementation class

* Query investment project fund source
public List<ProjectBasicInfoDTO> queryFundingSource() {
//Query investment project fund source
List<DictionaryBean> list = projectBasicInfoMapper.queryDictionaryBean(Constant.FUNDING_SOURCE,Constant.DELFLAG);
List<ProjectBasicInfoDTO> dataProcessing = ProjectBasicInfoServiceImpl.dataProcessing(list);
List<ProjectBasicInfoDTO> buildByRecursive = TreeUtils.buildByRecursive(dataProcessing);
return buildByRecursive;
* Place dictionary data traversal into the projectbasicinfo dto entity
* @param list
* @return
public static List<ProjectBasicInfoDTO> dataProcessing(List<DictionaryBean> list){
List<ProjectBasicInfoDTO> list1 = Lists.newArrayList();
for (DictionaryBean dictionaryBean : list) {
ProjectBasicInfoDTO projectBasicInfoDTO = new ProjectBasicInfoDTO();
return list1;

Recursive method tool class

package cn.ccccltd.ccccoi.mediumterminvestment.util;
import java.util.ArrayList;
import java.util.List;
import cn.ccccltd.ccccoi.mediumterminvestment.domain.ProjectBasicInfoDTO;
public class TreeUtils {
* Using recursive method to build tree
* @param treeNodes
* @return
public static List<ProjectBasicInfoDTO> buildByRecursive(List<ProjectBasicInfoDTO> projectBasicInfoDTOs) {
List<ProjectBasicInfoDTO> trees = new ArrayList<ProjectBasicInfoDTO>();
for (ProjectBasicInfoDTO projectBasicInfoDTO : projectBasicInfoDTOs) {
if ("".equals(projectBasicInfoDTO.getpId()) || projectBasicInfoDTO.getpId() == null ) {
return trees;
* Recursively find child nodes
* @param treeNodes
* @return
public static ProjectBasicInfoDTO findChildren(ProjectBasicInfoDTO projectBasicInfoDTO,List<ProjectBasicInfoDTO> projectBasicInfoDTOs) {
for (ProjectBasicInfoDTO projectBasicInfoDTO2 : projectBasicInfoDTOs) {
if(projectBasicInfoDTO.getId().equals(projectBasicInfoDTO2.getpId())) {
if(projectBasicInfoDTO.getChildren() == null) {
projectBasicInfoDTO.setChildren(new ArrayList<ProjectBasicInfoDTO>());
//Whether there are any child nodes. If so, continue to traverse. If not, return directly
return projectBasicInfoDTO;

Tree structure rendering

The above is a small demo of background processing tree structure, focusing on the recursive tool class.

Original link:

Source network, only for learning, if there is infringement, contact delete.

Tags: Programming Java network

Posted on Mon, 23 Mar 2020 10:24:23 -0400 by neogranas