示例代码
@Test
public void fun1(){
//1.创建, 调用空参构造函数
Configuration conf = new Configuration();
//2.读取指定配置文件
conf.configure();
//3.读取指定orm元数据(拓展)
// conf.addResource(resourceName);
// conf.addClass(persistentClass);
//根据配置信息创建SessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
//4.获取Session对象
Session session = sessionFactory.openSession();
//获得一个与线程绑定的Session对象
// sessionFactory.getCurrentSession();
//5.Session获得操作事务的Transaction对象
// Transaction tx = session.getTransaction();
//开启事务并获得操作事务的transaction对象(建议使用)
Transaction tx = session.beginTransaction();
//------------------------------------------
Customer c = new Customer();
c.setCust_name("王大锤");
session.save(c);
//------------------------------------------
//提交事务
tx.commit();
//回滚事务
tx.rollback();
//释放资源
session.close();
sessionFactory.close();
}