表结构
create table common_tree
(
id bigint not null comment '主键' primary key,
p_id bigint null comment '父节点id',
tree_code varchar(100) null comment '树形区分',
tree_describe varchar(255) null comment '树形描述',
node_name varchar(200) null comment '节点名称',
node_value varchar(100) null comment '节点值',
node_index int null comment '排序'
)
comment '树形结构表';
代码
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
*
*
* @author : mc
* @date : 2023-03-15 10:20
*/
@Service
public class CommonTreeBiz {
@Resource
private UCommonTreeMapper treeMapper;
public List<UCommonTree> buildTree(CommonTreeCodeEnum treeCodeEnum) {
List<UCommonTree> treeList = new ArrayList<>();
List<UCommonTree> trees = selectTree(treeCodeEnum);
for (UCommonTree tree : this.getRootNode(trees)) {
this.buildChilTree(tree, trees);
treeList.add(tree);
}
return treeList;
}
private List<UCommonTree> selectTree(CommonTreeCodeEnum treeCodeEnum) {
Example example = new Example(UCommonTree.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("treeCode", treeCodeEnum.getCode());
criteria.andEqualTo("disableFlag", "1");
example.setOrderByClause("node_index asc");
return treeMapper.selectByExample(example);
}
private List<UCommonTree> getRootNode(List<UCommonTree> trees) {
List<UCommonTree> rootList = new ArrayList<>();
for (UCommonTree tree : trees) {
if (tree.getPId().equals("0")) {
rootList.add(tree);
}
}
return rootList;
}
private UCommonTree buildChilTree(UCommonTree tree, List<UCommonTree> list) {
List<UCommonTree> treeList = new ArrayList<>();
for (UCommonTree t : list) {
if (t.getPId().equals(tree.getId())) {
treeList.add(this.buildChilTree(t, list));
}
}
tree.setChildren(treeList);
return tree;
}
}
标签:comment,Java,List,tree,树形,UCommonTree,treeList,null,结构
From: https://www.cnblogs.com/MC-Bonnie/p/18160234