标签:
public class User { private int id; private String name; private Date birthDay; //getter setter… }
Configuration cfg = new Configuration(); cfg.configure(“config.cfg.xml”); // 也可以通过cfg.setProperty设置属性。 SessionFactory sessionFactory = cfg.buildSessionFactory();
所以将以上代码封装成工具类:
HibernateUtils.java
package com.dzq.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUntils { private static SessionFactory sessionFactory; private HibernateUntils() { } static { Configuration cfg = new Configuration(); cfg.configure();//如果不是hibernate.cfg.xml这个文件名,需要加上文件名 sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession(){ return sessionFactory.openSession(); } }
public static void addUser(User user){ Session s=null; Transaction tx=null; try { s=HibernateUntils.getSession(); tx=s.beginTransaction(); s.save(user); tx.commit(); } catch (Exception e) { if(tx!=null) tx.rollback(); throw new RuntimeException(e); }finally{ if(s!=null){ s.close(); } } }
package com.dzq.utils; import java.io.Serializable; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class HibernateUntils { private static SessionFactory sessionFactory; private HibernateUntils() { } static { Configuration cfg = new Configuration(); cfg.configure();//如果不是hibernate.cfg.xml这个文件名,需要加上文件名 sessionFactory = cfg.buildSessionFactory(); } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession(){ return sessionFactory.openSession(); } /** * 添加 * @param entity */ public static void add(Object entity) { Session s = null; Transaction tx = null; try { s = HibernateUntils.getSession(); tx = s.beginTransaction(); s.save(entity); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e); } finally { if (s != null) { s.close(); } } } /** * 修改 * @param entity */ public static void update(Object entity) { Session s = null; Transaction tx = null; try { s = HibernateUntils.getSession(); tx = s.beginTransaction(); s.update(entity); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e); } finally { if (s != null) { s.close(); } } } /** * 删除 * @param entity */ public static void delete(Object entity) { Session s = null; Transaction tx = null; try { s = HibernateUntils.getSession(); tx = s.beginTransaction(); s.delete(entity); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw new RuntimeException(e); } finally { if (s != null) { s.close(); } } } /** * 根据主键id查询 * @param clazz * @param id * @return */ public static Object get(Class clazz,Serializable id) { Session s = null; try { s = HibernateUntils.getSession(); Object obj=s.get(clazz, id); return obj; } finally { if (s != null) { s.close(); } } } }
标签:
原文地址:http://www.cnblogs.com/xiaoduc-org/p/5463011.html