利用递归算法,把一个文件的所有文件都保存到数据库中,并在控制台打印出来

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.getChildrens().size()+"]"));
			}
			Set nodeList=root.getChildrens();
			//List nodeList=session.createQuery("select a from Node a where pid="+root.getId()).list();
			for(Iterator it=nodeList.iterator();it.hasNext();){
				Node node=(Node) it.next();
				printAllNode(node,session);
			}

		
	}
	
}

MainTest.java

package com.pk.tree;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class MainTest {
	public static void main(String[] args) {
		Configuration cfg=new Configuration().configure();
		SchemaExport export=new SchemaExport(cfg);
		export.create(true, true);
	}
}

TestTree.java

package com.pk.test;

import com.pk.tree.NodeCreate;

import junit.framework.TestCase;

public class TestTree extends TestCase {
	public void testSave(){
		NodeCreate.getInstance().createTree("E:\\aa\\workspace\\hibernate_8_many2many\\");
	}
	public void testPrint(){
		NodeCreate.getInstance().printNode(1);
	}
}

HibernateUtil.java

package com.pk.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static SessionFactory sessionFactory;
	
	static{
		try{
			Configuration cfg=new Configuration().configure();
			sessionFactory=cfg.buildSessionFactory();
		}catch(HibernateException e){
			e.printStackTrace();
			throw new ExceptionInInitializerError();
		}
	}
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	public static Session getSession(){
		return sessionFactory.openSession();
	}
	public static void closeSession(Session session){
		if(session!=null){
			if(session.isOpen()){
				session.close();
			}
		}
	}
	public static void closeSessionFactory(){
		if(sessionFactory!=null){
			sessionFactory.close();
		}
	}
}

猜你喜欢

转载自share.iteye.com/blog/1102742