增:
//获得session
Session session = HibernateUtil.getSession();
//获得事物
Transaction tx = session.beginTransaction();
//构造对象
User user = new User();
user.setName("tmack81888");
user.setPwd("9090909090");
session.save(user);
//6.提交事务
tx.commit();
//7.关闭session
HibernateUtil.closeSession();
删:
//删除对象
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user = (User) session.get(User.class, 1);
//严谨一些
if (user!=null){
session.delete(user);
}
tx.commit();
session.close();
改:
//更新对象
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user = session.get(User.class,5);
user.setName("woshisheia");
session.update(user);
tx.commit();
session.close();
查:
User user = new User();
user = session.get(User.class,5);
#注意组合主键表怎样获取这个对象
ScoreId sid = new ScoreId();
sid.setStuId(1);
sid.setSubjectId(8);
ScoreResult sr = new ScoreResult();
sr = (ScoreResult)session.get(ScoreResult.class,sid);