转自 http://together.javaeye.com/blog/29482?page=2
在系统中,经常会用到无限级的树形结构分类,如组织机构管理、商品/地区分类等等。在以前的一个贴子:http://www.javaeye.com/topic/26987“复杂商品分类的表如何建立?”中,讨论过树形无级分类的实现方法。
一般无外采用两种方式,
一是类似struts-menu(http://struts-menu.sourceforge.net)的XML文件管理方式,配置起来比较方便,但很难与系统中其它应用数据集成;
二是使用数据库存储,定义父子关系。
在我们现在开发的一个产品中,使用hibernate实现了一套树形结构的处理方法,实现了树的基本操作,上溯、下溯、子节点的添加/移除和递归查找、对象关联等。简介如下:
■适用范围,具有树形特征的所有对象,如树形菜单、组织结构、信息分类、论坛主贴与回复等。
■完整源码下载(内置了hsql数据库及测试数据,正式使用时请将war置于APPSERVER的webapps目录下,修改解包后的WEB-INF/classes/hibernate.cfg.xml,编辑其中hsqldb的物理路径。如jdbc:hsqldb:file:c:/tomcat5/webapps/treetest/db/test)见附件,运行http://ServerName:ServerPort/treetest/menumanage.do。
■树形结构显示,使用的是xtree。为便于编辑维护,自己写了一个左键弹出菜单(xtree的右键事件无法更改),进行节点的添加、修改、删除、转移操作。(PS:这套维护界面是完全跨浏览器的,有兴趣的不妨一试)
■关联关系:
可以使用objects对象来配置关联关系,实现多对多/一对多等关系。在BaseTree中,getObjects()方法是abstract的,可以根据需要自己定义。如论坛分类与每个分类所对应的贴子相关联,商品分类与商品编码相关联等,可以根据需要来处理hbm文件。若需要多项关联,亦可扩展。如菜单与用户、部门、岗位分别进行关联
■hibernate2.1.7的一个bug,在这个测试源码的dao中,TreeManager的getRoots方法,
session.createQuery(" from " + cls.getName() + " where enabled=? and parent_id is null order by id");
在hibernate2中必须像写成parent_id is null,才能正确运行,这应该是2.1.7中的一个bug。而hibernate3中,可以使用parent is null的hsql。
■主要代码:
继承关系如下,假如要实现国家分类:
CountryTree extends BaseTree(abstract class)
BaseTree(abstract class) implements Tree(interface)
为节省版面,下面代码去掉了javadoc
Tree.java
- /**
- * 实现了树的基本操作,上溯、下溯、子节点的添加/移除和递归查找、对象关联等
- */
- package test.testtree.base;
- import java.util.Set;
- public interface Tree {
- public String getCode();
- public String getName();
- public String getDescription();
- public Tree getParent();
- public Set getParents();
- public boolean isRoot();
- public boolean isLeaf();
- public boolean isParentOf(Tree tree);
- public boolean isChildOf(Tree tree);
- public void addChild(Tree tree);
- public void rmChild(Tree tree);
- public Set getAllChildren();
- public Set getChildren();
- public Set getAllLeaves();
- public void addObject(Object obj);
- public void rmObject(Object obj);
- public Set getObjects();
- public Long getId();
- }
/**
* 实现了树的基本操作,上溯、下溯、子节点的添加/移除和递归查找、对象关联等
*/
package test.testtree.base;
import java.util.Set;
public interface Tree {
public String getCode();
public String getName();
public String getDescription();
public Tree getParent();
public Set getParents();
public boolean isRoot();
public boolean isLeaf();
public boolean isParentOf(Tree tree);
public boolean isChildOf(Tree tree);
public void addChild(Tree tree);
public void rmChild(Tree tree);
public Set getAllChildren();
public Set getChildren();
public Set getAllLeaves();
public void addObject(Object obj);
public void rmObject(Object obj);
public Set getObjects();
public Long getId();
}
BaseTree.java
- package test.testtree.base;
- import java.util.*;
- public abstract class BaseTree extends BasePojo implements Tree{
- protected String code;
- protected String name;
- protected String description;
- protected BaseTree parent;
- protected Set children = new HashSet();
- protected Set objects = new HashSet();
- public void setCode(String code) {
- this.code = code;
- }
- abstract public String getCode();
- public void setName(String name) {
- this.name = name;
- }
- abstract public String getName();
- public void setDescription(String description) {
- this.description = description;
- }
- abstract public String getDescription();
- abstract public Tree getParent();
- public boolean isRoot() {
- return (getParent()==null);
- }
- public boolean isLeaf() {
- return (this.getChildren().size()==0);
- }
- public boolean isParentOf(Tree tree) {
- if (tree==null || ((BaseTree) tree).equals(this)) {
- /*如果对方为空*/
- return false;
- }else if(this.isLeaf()){
- /*如果自己为叶子,则返回FALSE*/
- return false;
- }else if(tree.isRoot()){
- /*如果对方为根,返回FALSE*/
- return false;
- }else{
- BaseTree bt = (BaseTree) (tree.getParent());
- if (this.equals(bt)){
- /*如果对方的父节点是自己,则返回TRUE*/
- return true;
- }else{
- /*判断对方的父节点是否是自己的孩子,进行递归*/
- return isParentOf(bt);
- }
- }
- }
- public boolean isChildOf(Tree tree) {
- return (tree.isParentOf(this));
- }
- public void addChild(Tree tree) {
- children.add(tree);
- }
- public void rmChild(Tree tree) {
- children.remove(tree);
- ((BaseTree) tree).setParent(null);
- }
- public Set getAllLeaves() {
- Set set_old = this.getAllChildren();
- Set set = new HashSet();
- set.addAll(set_old);
- Iterator itr = set_old.iterator();
- while(itr.hasNext()){
- BaseTree bt = (BaseTree) itr.next();
- if (! bt.isLeaf()){
- set.remove(bt);
- }
- }
- return set;
- }
- public Set getParents(){
- Set parents = new HashSet();
- Tree p = this.getParent();
- if(p!=null){
- parents.add(p);
- parents.addAll(p.getParents());
- }
- return parents;
- }
- public Set getAllChildren() {
- Set set = new HashSet();
- Stack stack = new Stack();
- stack.push(this);
- while(!stack.empty()){
- BaseTree bt = (BaseTree) stack.pop();
- set.add(bt);
- Iterator itr = bt.getChildren().iterator();
- while(itr.hasNext()){
- BaseTree btchild = (BaseTree) itr.next();
- stack.push(btchild);
- }
- }
- set.remove(this);
- return set;
- }
- abstract public Set getChildren();
- public void addObject(Object obj) {
- objects.add(obj);
- }
- public void rmObject(Object obj) {
- objects.remove(obj);
- }
- abstract public Set getObjects();
- public void setParent(Tree parent) {
- this.parent = (BaseTree) parent;
- }
- public void setChildren(Set children) {
- this.children = children;
- }
- public void setObjects(Set objects) {
- this.objects = objects;
- }
- }
package test.testtree.base;
import java.util.*;
public abstract class BaseTree extends BasePojo implements Tree{
protected String code;
protected String name;
protected String description;
protected BaseTree parent;
protected Set children = new HashSet();
protected Set objects = new HashSet();
public void setCode(String code) {
this.code = code;
}
abstract public String getCode();
public void setName(String name) {
this.name = name;
}
abstract public String getName();
public void setDescription(String description) {
this.description = description;
}
abstract public String getDescription();
abstract public Tree getParent();
public boolean isRoot() {
return (getParent()==null);
}
public boolean isLeaf() {
return (this.getChildren().size()==0);
}
public boolean isParentOf(Tree tree) {
if (tree==null || ((BaseTree) tree).equals(this)) {
/*如果对方为空*/
return false;
}else if(this.isLeaf()){
/*如果自己为叶子,则返回FALSE*/
return false;
}else if(tree.isRoot()){
/*如果对方为根,返回FALSE*/
return false;
}else{
BaseTree bt = (BaseTree) (tree.getParent());
if (this.equals(bt)){
/*如果对方的父节点是自己,则返回TRUE*/
return true;
}else{
/*判断对方的父节点是否是自己的孩子,进行递归*/
return isParentOf(bt);
}
}
}
public boolean isChildOf(Tree tree) {
return (tree.isParentOf(this));
}
public void addChild(Tree tree) {
children.add(tree);
}
public void rmChild(Tree tree) {
children.remove(tree);
((BaseTree) tree).setParent(null);
}
public Set getAllLeaves() {
Set set_old = this.getAllChildren();
Set set = new HashSet();
set.addAll(set_old);
Iterator itr = set_old.iterator();
while(itr.hasNext()){
BaseTree bt = (BaseTree) itr.next();
if (! bt.isLeaf()){
set.remove(bt);
}
}
return set;
}
public Set getParents(){
Set parents = new HashSet();
Tree p = this.getParent();
if(p!=null){
parents.add(p);
parents.addAll(p.getParents());
}
return parents;
}
public Set getAllChildren() {
Set set = new HashSet();
Stack stack = new Stack();
stack.push(this);
while(!stack.empty()){
BaseTree bt = (BaseTree) stack.pop();
set.add(bt);
Iterator itr = bt.getChildren().iterator();
while(itr.hasNext()){
BaseTree btchild = (BaseTree) itr.next();
stack.push(btchild);
}
}
set.remove(this);
return set;
}
abstract public Set getChildren();
public void addObject(Object obj) {
objects.add(obj);
}
public void rmObject(Object obj) {
objects.remove(obj);
}
abstract public Set getObjects();
public void setParent(Tree parent) {
this.parent = (BaseTree) parent;
}
public void setChildren(Set children) {
this.children = children;
}
public void setObjects(Set objects) {
this.objects = objects;
}
}