一、搭建Junit环境测试,加入junit-4.7.jar,然后建立一个TeacherTest的测试类,
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class TeacherTest {
private static SessionFactory sf=null;
@BeforeClass
public static void createSessionFactory(){
sf=new AnnotationConfiguration().configure().buildSessionFactory();
}
@Test
public void testSave(){
Session session=sf.openSession();
session.beginTransaction();
Teacher t=new Teacher();
t.setId(8);
t.setName("wang");
t.setTitle("教授");
session.save(t);
session.beginTransaction().commit();
session.close();
}
@AfterClass
public static void closeSessionFactory()
{
sf.close();
}
}