//测试类
public class studentsTest
{private Configuration config;// 1.声明私有配置对象类
private ServiceRegistry serviceRegistry;// 2.声明私有服务注册对象类
private SessionFactory sessionFactory;// 3.声明私有会话工厂对象类
private Session session;// 4.声明私有会话对象类
private Transaction transaction;// 5.声明私有事务对象类
@Before
public void beforeStudent() {
// 创建配置对象
config = new Configuration().configure();
// 创建服务注册对象
serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
// 创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
// 打开会话
session = sessionFactory.openSession();
// 开始事务
transaction = session.beginTransaction();
}
@After
public void afterStudent() {
// 事务提交
transaction.commit();
// 会话关闭
session.close();
// 会话工厂关闭
sessionFactory.close();
}
@Test
public void testStudentHbmXml() {
Student student = new Student(1, "黄", "男");
session.save(student);
System.out.println("测试中...");
}
}
}
转载于:https://www.cnblogs.com/Jxiaobai/p/6618595.html