junit学习(九)——Hibernate的单元测试
程序员文章站
2022-06-23 18:18:24
...
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用于连接数据库。代码如下:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.SQLServerDialect </property> <property name="connection.url"> jdbc:sqlserver://127.0.0.1:1433;databaseName=test </property> <property name="connection.username">sa</property> <property name="connection.password">sa</property> <property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver </property> </session-factory> </hibernate-configuration>
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<Session> threadLocal = new ThreadLocal<Session>(); 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已经添加到项目中。