当前位置:编程学习 > JAVA >>

hibernate工具类封装增删改操作,简化编程

public class HibernateUtil { 
    private static Configuration cfg; 
    private static SessionFactory sessionFactory; 
     
    private HibernateUtil(){} 
     
    static{ 
        cfg = new Configuration(); 
        cfg.configure(); 
        sessionFactory = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry()); 
    } 
     
    public static SessionFactory getSessionFactory() { 
        return sessionFactory; 
    } 
 
    public static synchronized Session getSession(){ 
        return sessionFactory.openSession(); 
    } 
     
    /*
     * 公用增删改操作
     */ 
     
    public static boolean save(Object obj){ 
        Session session = null; 
        Transaction trans = null; 
        try{ 
            session = HibernateUtil.getSession(); 
            trans = session.beginTransaction(); 
            session.save(obj); 
            trans.commit(); 
            return true; 
        }catch(HibernateException e){ 
            if(null != trans) 
                trans.rollback(); 
            e.printStackTrace(); 
        }finally{ 
            if(null != session) 
                session.close(); 
        } 
        return false; 
    } 
     
    public static boolean delete(Object obj){ 
        Session session = null; 
        Transaction trans = null; 
        try{ 
            session = HibernateUtil.getSession(); 
            trans = session.beginTransaction(); 
            session.delete(obj); 
            trans.commit(); 
            return true; 
        }catch(HibernateException e){ 
            if(null != trans) 
                trans.rollback(); 
            e.printStackTrace(); 
        }finally{ 
            if(null != session) 
                session.close(); 
        } 
        return false; 
    } 
     
    public static boolean update(Object obj){ 
        Session session = null; 
        Transaction trans = null; 
        try{ 
            session = HibernateUtil.getSession(); 
            trans = session.beginTransaction(); 
            session.update(obj); 
            trans.commit(); 
            return true; 
        }catch(HibernateException e){ 
            if(null != trans) 
                trans.rollback(); 
            e.printStackTrace(); 
        }finally{ 
            if(null != session) 
                session.close(); 
        } 
        return false; 
    } 
    @SuppressWarnings("rawtypes") 
    public static Object get(Class clazz,Serializable id){ 
        Session session = null; 
        Object obj = null; 
        try{ 
            session = HibernateUtil.getSession(); 
            obj = session.get(clazz,id); 
        }catch(HibernateException e){ 
            e.printStackTrace(); 
        }finally{ 
            if(null != session) 
                session.close(); 
        } 
        return obj; 
    } 
     

 

 

摘自 mzlqh的专栏

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,