Hibernate封装类

来源:互联网 发布:淘宝限制购买怎么解决 编辑:程序博客网 时间:2024/05/20 07:36
 

Hibernate封装类

 8495人阅读 评论(6) 收藏 举报
hibernatesessionexceptionpropertiesobjectnull

 最近研究Hibernate,写了一个对Hibernate数据访问的一个封装,现在贴出来,供大家分析讨论。

[java] view plaincopyprint?
  1. package com.lynn.db.dao;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.Configuration;  
  6.   
  7. public class BaseDao {  
  8.       
  9.     static Configuration conf = null;  
  10.     static SessionFactory factory = null;  
  11.     static Session session = null;  
  12.     static{  
  13.         try {  
  14.             conf = new Configuration().configure();  
  15.             factory = conf.buildSessionFactory();  
  16.               
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  
  21.       
  22.     /** 
  23.      * 获得Session连接 
  24.      * */  
  25.     public static Session getSession(){  
  26.         session = factory.openSession();  
  27.         return session;  
  28.     }  
  29.     /** 
  30.      * 关闭session连接 
  31.      * */  
  32.     public static void closeSession(){  
  33.         if(session!=null)  
  34.             session.close();  
  35.     }  
  36.       
  37.   
  38. }  


下面是封装的增、删、改、查

[java] view plaincopyprint?
  1. package com.lynn.db.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.Criteria;  
  6. import org.hibernate.FetchMode;  
  7. import org.hibernate.Session;  
  8. import org.hibernate.Transaction;  
  9. import org.hibernate.criterion.Order;  
  10. import org.hibernate.criterion.Restrictions;  
  11.   
  12. import com.lynn.page.PageInfo;  
  13.   
  14.   
  15. public class HibernateDao {  
  16.       
  17.     protected Session session = null;  
  18.     protected Transaction ts = null;  
  19.     protected Criteria criteria;  
  20.       
  21.     /** 
  22.      * session保存方法 
  23.      * */  
  24.     public boolean save(Object obj){  
  25.         boolean check = false;  
  26.         try {  
  27.             session = BaseDao.getSession();//获得连接  
  28.             ts = session.beginTransaction();//创建事务  
  29.               
  30.             session.save(obj);//保存对象  
  31.             ts.commit();//提交事务  
  32.             check = true;  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.             ts.rollback();//出现异常则回滚事务  
  36.         }  
  37.         return check;  
  38.     }  
  39.     /** 
  40.      * session删除方法 
  41.      * */  
  42.     public boolean  delete(Object obj){  
  43.         boolean check = false;  
  44.         try {  
  45.             session = BaseDao.getSession();  
  46.             ts = session.beginTransaction();  
  47.             session.delete(obj);  
  48.             ts.commit();  
  49.             check = true;  
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.             ts.rollback();  
  53.         }  
  54.         return check;  
  55.     }  
  56.       
  57.     /** 
  58.      * session修改方法 
  59.      * */  
  60.     public boolean update(Object obj){  
  61.         boolean check = false;  
  62.         try {  
  63.             session = BaseDao.getSession();  
  64.             ts = session.beginTransaction();  
  65.             session.delete(obj);  
  66.             ts.commit();  
  67.             check = true;  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.             ts.rollback();  
  71.         }  
  72.         return check;  
  73.     }  
  74.     /** 
  75.      * session查询方法(抓取)查询所有 
  76.      * @return Criteria 
  77.      * */  
  78.     public Criteria QueryByFetch(String...fetch){  
  79.           
  80.         try {  
  81.               
  82.             if(fetch!=null && fetch.length>0){  
  83.                 for (int i = 0; i < fetch.length; i++) {  
  84.                     criteria = criteria.setFetchMode(fetch[i], FetchMode.JOIN);  
  85.                 }  
  86.             }  
  87.         } catch (Exception e) {  
  88.             e.printStackTrace();  
  89.         }  
  90.         return criteria;   
  91.     }  
  92.       
  93.     /** 
  94.      * 根据单条件或者多条件查询 
  95.      * 至少2个参数,第一个为属性名,第二个为属性值 
  96.      * @return Criteria 
  97.      * */  
  98.     public Criteria QueryByProperties(Object...properties){  
  99.           
  100.         try {  
  101.             if(properties!=null && properties.length>0){  
  102.                 for (int i = 0; i < properties.length; i=i+2) {  
  103.                     criteria = criteria.add(Restrictions.like((String)properties[i], properties[i+1]));  
  104.                 }  
  105.                   
  106.             }  
  107.             return criteria;  
  108.         } catch (Exception e) {  
  109.             e.printStackTrace();  
  110.         }  
  111.         return null;  
  112.     }  
  113.       
  114.     /** 
  115.      * 查询所有 
  116.      * */  
  117.     public Criteria Query(Class c){  
  118.         session = BaseDao.getSession();  
  119.         criteria = session.createCriteria(c);  
  120.         return criteria;  
  121.     }  
  122.     /** 
  123.      * 将查询结果返回List 
  124.      * @return List 
  125.      * */  
  126.     public List QueryList(){  
  127.         try {  
  128.             return criteria.list();  
  129.         } catch (Exception e) {  
  130.             e.printStackTrace();  
  131.         }  
  132.         return null;  
  133.     }  
  134.     /** 
  135.      * 查询单个数据 
  136.      * @return Object 
  137.      * */  
  138.     public Object QueryUnique(){  
  139.         try {  
  140.             return criteria.uniqueResult();  
  141.         } catch (Exception e) {  
  142.             e.printStackTrace();  
  143.         }  
  144.         return null;  
  145.     }  
  146.     /** 
  147.      * 此类专门用于分页 
  148.      * */  
  149.     public Criteria QueryByPage(PageInfo pageInfo){  
  150.           
  151.            
  152.         return criteria.setFirstResult((pageInfo.getPageIndex()-1)* pageInfo.getPageSize())  
  153.         .setMaxResults(pageInfo.getPageSize());  
  154.           
  155.     }  
  156.     /** 
  157.      * 此类专门用于排序 
  158.      * */  
  159.     public Criteria QueryByOrder(Order o){  
  160.         return criteria.addOrder(o);  
  161.     }  
  162.       
  163. }  
0 0
原创粉丝点击