• 【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);
        }
    }
  • 相关阅读:
    [转]Entity Framework 和NHibernate的区别
    NHibernate One Session Per Request简单实现
    memcache和memcached之间的区别
    Memcached 内存分配机制介绍
    linux,apache,php,mysql常用的查看版本信息的方法
    Linux查看文件夹大小
    Linux查看系统配置常用命令
    php 文件缓存(转)
    memcache在xampp下的安装
    通过改进代码将for循环遍历数组提高效率
  • 原文地址:https://www.cnblogs.com/549294286/p/2828559.html
Copyright © 2020-2023  润新知