public class Test01 { private ServiceRegistry sr =null; private Session se =null; private Transaction ts = null; private SessionFactory sf = null; //在测试用例方法被执行之前自动执行的方法 //一般用来初始化公用的对象 //前置方法 @Before public void init() { //1.获取配置文件 Configuration cfg = new Configuration().configure(); //2.注册配置 sr =new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()).build(); //3. 获取Session-Factory(相当于JDBCd 连接) sf = cfg.buildSessionFactory(sr); System.out.println(sf); //4.产生Session(回话) se = sf.openSession(); //创建 //5.启动事务 ts = se.beginTransaction(); } //后置方法 //一般用来释放资源 @After public void destroy() { //7.提交事务 ts.commit(); //8释放资源 se.close(); sf.close(); } }