//首先获得SessionFactory的对象
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
//然后获得session的对象
Session s = sessionFactory.openSession();
//其次获得Transaction的对象
Transaction transaction = s.beginTransaction();
//执行相关的数据库操作:增、删、改、查;
session.save(user);
session.delete(user);
session.update(user);
Query<R> query = session.createQuery("from User");//查询
List list = query.list();
//提交事务
transaction.commit();
//如果有异常,我们还要作事务的回滚,恢复到操作之前;
transaction.rollback();
//最后还要关闭session释放资源;
session.close();