这是商品管理页面.
商品分类是:大类-->一级分类-->二级分类-->品牌-->产品.
有一个需求是,当我只选择了大类(手机/数码/配件)和一级分类(手机通讯),我希望商品展示页面能够展示手机通讯下面所有的产品.
也就是说需要寻找分类表里手机通讯下的品牌分类节点.
分类表是树形结构.
本来想用MySQL的函数或者存储过程实现.但是感觉操作数据比较麻烦,不好写,当然原因是我不太精通其编写...
所以还是决定把分类表id和pid读取出来,再用Java去实现这个需求.
所以写了下面的测试例子:
树结构实体类,包含三个属性id(子节点),pid(父节点),name(名称).
class TreeNode{
int id;
int pid;
String name;
public TreeNode() {
super();
}
public TreeNode(int id, int pid) {
super();
this.id = id;
this.pid = pid;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//用于存放所有的子节点和终结点
//执行函数treeMenuList后childNode和lastChildNode都会存在重复值,因此需要去重处理
List childNode = new ArrayList();
List lastChildNode = new ArrayList();
public List treeMenuList(List treeNodes ,int pid) {
//
List tempTreeNode = new ArrayList();
List tempTreeNode1 = new ArrayList();
for(TreeNode node : treeNodes) {
if(node.getPid() == pid) {
//说明存在子节点
tempTreeNode1 = treeMenuList(treeNodes,node.getId());
if(tempTreeNode1.isEmpty()) {
//不存在子节点
lastChildNode.add(node);
}
childNode.add(node);
//用于让上一级判断是否存在子节点
//因为存在子节点则tempTreeNode不为空
//函数结束后返回tempTreeNode给上一级以供判断
tempTreeNode.add(node);
System.out.println("当前节点存在子节点");
}
}
return tempTreeNode;
}
树的结构如下:
0---1----4
+---5
+---2---6
+---7
+---3---8---15
+---9---10---11
+---12
+---13
+---14
@Test
public void test() {
List treeNodes = new ArrayList();
treeNodes.add(new TreeNode(1,0));
treeNodes.add(new TreeNode(2,0));
treeNodes.add(new TreeNode(3,0));
treeNodes.add(new TreeNode(4,1));
treeNodes.add(new TreeNode(5,1));
treeNodes.add(new TreeNode(6,2));
treeNodes.add(new TreeNode(7,2));
treeNodes.add(new TreeNode(8,3));
treeNodes.add(new TreeNode(9,3));
treeNodes.add(new TreeNode(15,8));
treeNodes.add(new TreeNode(10,9));
treeNodes.add(new TreeNode(11,10));
treeNodes.add(new TreeNode(12,10));
treeNodes.add(new TreeNode(13,10));
treeNodes.add(new TreeNode(14,10));
int rootId = 3;
System.out.println("查找 rootId = " + rootId + "的子节点");
treeMenuList(treeNodes,rootId);
for(TreeNode node : childNode) {
System.out.println(" childNode id = " + node.getId());
}
childNode = removeStringListDupli(childNode);
System.out.println("childNode去除重复值后");
for(TreeNode node : childNode) {
System.out.println("childNode id = " + node.getId());
}
for(TreeNode node : lastChildNode) {
System.out.println("lastChildNode id = " + node.getId());
}
lastChildNode = removeStringListDupli(lastChildNode);
System.out.println("lastChildNode去除重复值后");
for(TreeNode node : lastChildNode) {
System.out.println("lastChildNode id = " + node.getId());
}
}
树的结构如下:
0---1----4
+---5
+---2---6
+---7
+---3---8---15
+---9---10---11
+---12
+---13
+---14
查找 rootId = 3 的子节点
childNode去除重复值后
childNode id = 15
childNode id = 8
childNode id = 11
childNode id = 12
childNode id = 13
childNode id = 14
childNode id = 10
childNode id = 9
lastChildNode去除重复值后
lastChildNode id = 15
lastChildNode id = 11
lastChildNode id = 12
lastChildNode id = 13
lastChildNode id = 14