zoukankan      html  css  js  c++  java
  • 【hibernate】HibernateUtil和Hibernate的DAO

    HibernateUtil

    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
        private static final SessionFactory sessionFactory;
        private static final ThreadLocal m_session = new ThreadLocal();  
        
        static {
            try {
                Configuration config = new Configuration();
                config.addFile("src\\main\\resources\\hibernate.cfg.xml").configure();
                sessionFactory = config.buildSessionFactory();
            } catch (HibernateException ex) {
                throw new RuntimeException("创建SessionFactory失败: " + ex.getMessage(), ex);  
            }
        }
        
        public static Session currentSession() throws HibernateException {  
            Session s = (Session) m_session.get();  
            if (s == null) {  
                s = sessionFactory.openSession();  
                m_session.set(s);  
            }  
            return s;  
        }  
          
        public static void closeSession() throws HibernateException {  
            Session s = (Session) m_session.get();  
            m_session.set(null);  
            if (s != null)  
                s.close();  
        }  
    }

    Hibernate的DAO

    import java.util.List;
    
    import org.ccnt.health.znserver.entry.NormalIll;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    
    public class NormalIllDAO {
        
        public void insertNormal(NormalIll nill){
            Session session = HibernateUtil.currentSession();
            Transaction tx = session.beginTransaction();
            session.save(nill);
            tx.commit();
            HibernateUtil.closeSession();
        }
        
        public void insertNormalList(List<NormalIll> li){
            for (NormalIll e : li){
                insertNormal(e);
            }
        }
        
        public List<NormalIll> getAll(){
            Session session = HibernateUtil.currentSession();
            String hql = "from NormalIll";
            Query query = session.createQuery(hql);
            List<NormalIll> list = query.list();
            return list;
        }
        
        public NormalIll getByName(String name){
            Session session = HibernateUtil.currentSession();
            String hql = "from NormalIll n where n.name = " + name;
            Query query = session.createQuery(hql);
            return (NormalIll)query.list().get(0);
        }
        
        public NormalIll getById(int id){
            Session session = HibernateUtil.currentSession();
            String hql = "from NormalIll n where n.id = " + id;
            Query query = session.createQuery(hql);
            return (NormalIll)query.list().get(0);
        }
    }
  • 相关阅读:
    跟我一起玩Win32开发(转自CSDN-东邪独孤)
    c指针讲解
    9×9扫雷游戏代码-C写的
    Winform GDI+ 相关资料
    hadoop datanode 和namenode之间的心跳
    打包 压缩 命令tar zip
    kudu 问题集
    配置NTP服务ntpd/ntp.conf
    kudu 1.8.0(开发版) 源码安装
    hue 集成spark+livy
  • 原文地址:https://www.cnblogs.com/549294286/p/2828559.html
Copyright © 2011-2022 走看看