Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
// 创建主键变量
pid = (Integer) session.save(user);
tx.commit();
} catch (RuntimeException e) {
if (tx != null)
tx.rollback();
throw e;
} finally {
session.close();
}
// 关闭sessionFactory
sessionFactory.close();
}
}
运行结果:
控制台:
20:47:57,366 DEBUG SQL:346 - insert intossh.c_user (age, firstname, lastname, address, zipcode, tel) values (?, ?, ?,?, ?, ?)
更多关注: