hibernate增加
@Override
public boolean add(PatientInfo pi) {
// TODO Auto-generated method stub
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
session.save(pi);
session.getTransaction().commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.getTransaction().rollback();
return false;
}
return true;
}
hibernate修改
@Override
public boolean update(PatientInfo pi) {
// TODO Auto-generated method stub
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
session.update(pi);
session.getTransaction().commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.getTransaction().rollback();
return false;
}
return true;
}
hibernate删除
@Override
public boolean delete(PatientInfo pi) {
// TODO Auto-generated method stub
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction();
session.delete(pi);
session.getTransaction().commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.getTransaction().rollback();
return false;
}
return true;
}
hibernate查询
@Override
public PatientInfo getPatientInfo(int u_id) {
// TODO Auto-generated method stub
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
PatientInfo result = null;
try {
session.beginTransaction();
result = session.get(PatientInfo.class,u_id);
session.getTransaction().commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.getTransaction().rollback();
return result;
}
return result;
}