日期:2014-05-16 浏览次数:20467 次
Node.java
package com.pk.tree; import java.util.Set; public class Node { private int id; private String name; private int level; //层次 private boolean leaf;//是否是叶子 private Node parents;//父节点 private Set childrens;//子节点 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public Node getParents() { return parents; } public void setParents(Node parents) { this.parents = parents; } public Set getChildrens() { return childrens; } public void setChildrens(Set childrens) { this.childrens = childrens; } }
?
?
Node.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.pk.tree"> <class name="Node" table="t_node"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="level"></property> <property name="leaf"></property> <many-to-one name="parents" column="pid"></many-to-one> <set name="childrens" inverse="true"> <key column="pid"></key> <one-to-many class="Node" /> </set> </class> </hibernate-mapping>
?
?
NodeCreate.java
package com.pk.tree; import java.io.File; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Session; import org.hibernate.Transaction; import com.pk.util.HibernateUtil; public class NodeCreate { private NodeCreate(){ } private static NodeCreate nodeCreate; public static synchronized NodeCreate getInstance(){ if(nodeCreate==null){ nodeCreate=new NodeCreate(); } return nodeCreate; } public void createTree(String filepath){ Session session=null; Transaction tx=null; try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); File root=new File(filepath); //真正的见表save(root,session,parent,int level) saveNode(root,session,null,1); tx.commit(); }catch(Exception e){ e.printStackTrace(); tx.rollback(); }finally{ HibernateUtil.closeSession(session); } } private void saveNode(File root,Session session ,Node parents,int level){ boolean leaf=root.isFile(); Node node=new Node(); node.setName(root.getName()); node.setLevel(level); node.setLeaf(leaf); node.setParents(parents); session.save(node); File[] files=root.listFiles(); if(files!=null&&files.length>0){ for(int i=0;i<files.length;i++){ saveNode(files[i],session,node,level+1); } } } public void printNode(int id){ Session session=null; Transaction tx=null; try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); Node root=(Node) session.load(Node.class, id); //打印其中的所有信息 printAllNode(root,session); tx.commit(); }catch(Exception e){ e.printStackTrace(); tx.rollback(); }finally{ HibernateUtil.closeSession(session); } } private void printAllNode(Node root,Session session){ boolean leaf=root.isLeaf(); //System.out.println(root.getId()+"********"+root.getName()); int level=root.getLevel(); if(level==1){ System.out.println(root.getName()); }else{ for(int i=0;i<level;i++){ System.out.print("| "); } System.out.println("--"+root.getName()+ (leaf ?"":"["+root.getChil