junit学习(九)——Hibernate的单元测试

阅读更多

1、在项目中添加Hibernate的Libraries

 

在项目上右击---->Properties---->Java Build  Path---->Libraries选项卡---->Add Library---->MyEclipse Libraries---->Hibernate x.x Core Libraries。我这里添加的是3.0版本。

 

2、在src下新建Hibernate的配置文件:hibernate.cfg.xml,新建一个session-factory用于连接数据库。代码如下:






	
		
			org.hibernate.dialect.SQLServerDialect
		
		
			jdbc:sqlserver://127.0.0.1:1433;databaseName=test
		
		sa
		sa
		
			com.microsoft.sqlserver.jdbc.SQLServerDriver
		
	

3、 在test资源包中新建HibernateSessionFactory类,用于读取Hibernate的配置文件

package com.wjl.junit;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
	private static final ThreadLocal threadLocal = new ThreadLocal();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

	static {
    	try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {
    }
	
    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();

		if (session == null || !session.isOpen()) {
			if (sessionFactory == null) {
				rebuildSessionFactory();
			}
			session = (sessionFactory != null) ? sessionFactory.openSession()
					: null;
			threadLocal.set(session);
		}

        return session;
    }

	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
	}

    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

	public static org.hibernate.SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static void setConfigFile(String configFile) {
		HibernateSessionFactory.configFile = configFile;
		sessionFactory = null;
	}

	public static Configuration getConfiguration() {
		return configuration;
	}
}

注:以上两个文件,在项目中添加Hibernate时会自动生成,可以先添加Hibernate,然后直接拷贝配置文件。

 

4、新建测试类进行测试

package com.wjl.junit;

import org.hibernate.Session;
import org.junit.Test;
/**
 * Junit_demo_12
 * Hibernate的单元测试
 * **/
public class HibernateTest {
	@Test
	public void test(){
		Session session = HibernateSessionFactory.getSession();
		System.out.println(session);//结果:org.hibernate.impl.SessionImpl(......),说明Hibernate已经添加到项目中
	}
}

说明Hibernate已经添加到项目中。

你可能感兴趣的:(Hibernate的单元测试)