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);
        }
    }
  • 相关阅读:
    一、分组查询
    续、传参
    页面加载时loading效果
    2019-6 21
    一、Nuget管理
    三、项目分析
    七、OIDC
    【2019-10-19】习惯的力量
    【2019-10-18】好好珍惜自己的好奇心
    【2019-10-17】女人有钱,体面又可爱
  • 原文地址:https://www.cnblogs.com/549294286/p/2828559.html
Copyright © 2011-2022 走看看