一種好的持久層開發方法——建立BaseDao和BaseDaoImpl


    使用hibernate開發持久層時,我們會發現:雖然entity類的含義和需求不同,其對應的Dao層類對應的方法也是不同的。但是有許多方法操作確實相同的。比如實體的增加,刪除,修改更新,以及許多常用的查詢方法。這些都是可復用的。因此可以把這些操作寫在一個BaseDao中,其他的dao都繼承於這個Dao。每個子dao只寫與自己的業務相關的方法,這樣可以提高代碼的復用,增加了開發效率,也方便今后可能的擴展。下面是我在我的項目中使用的BaseDao和BaseDaoImpl的使用方法。僅供參考:

BaseDao:

package com.bupt.auth.dao.base;

import java.io.Serializable;
import java.util.List;

public interface BaseDao<T> {
    Long save(T entity);  //保存實體類
    
    void delete(Long id); //刪除實體類

    void update(T entity); //更新實體

    T getById(Long id); //通過id獲得實體

    List<T> getByIds(Long[] ids);//根據id數組獲得對應的實體數組

    List<T> findAll();//獲得全部的實體

    Long totalNum();//實體類的數量
    
    List<T> getPage(int pageNow, int pageSize);//分頁查找
    
    List<T> find(String hql , String param);//根據具體的hql語句查找實體類


}

BaseDaoImpl:

  1 package com.bupt.auth.dao.base;
  2 
  3 import java.lang.reflect.ParameterizedType;
  4 import java.util.Collections;
  5 import java.util.List;
  6 
  7 import javax.annotation.Resource;
  8 
  9 import org.hibernate.Query;
 10 import org.hibernate.Session;
 11 import org.hibernate.SessionFactory;
 12 import org.springframework.transaction.annotation.Transactional;
 13 
 14 
 15 @Transactional
 16 @SuppressWarnings("unchecked")
 17 public abstract class BaseDaoImpl<T> implements BaseDao<T> {
 18     @Resource
 19     private SessionFactory sessionFactory;
 20 
 21     private Class<T> clazz=null;
 22     
 23     @SuppressWarnings("unchecked")
 24     public BaseDaoImpl(){
 25         ParameterizedType pt=(ParameterizedType)this.getClass().getGenericSuperclass();
 26         this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
 27     }
 28     public Session getSession() {
 29         return sessionFactory.getCurrentSession();
 30     }
 31     
 32     public void setSessionFactory(SessionFactory sessionFactory)
 33     {
 34         this.sessionFactory = sessionFactory;
 35     }
 36     public SessionFactory getSessionFactory()
 37     {
 38         return this.sessionFactory;
 39     }
 40     public Long save(T entity) {
 41         return    (Long)getSession().save(entity);
 42     }
 43 
 44     public void delete(Long id) {
 45         // TODO Auto-generated method stub
 46         Object object = getById(id);
 47         if(object != null){
 48             getSession().delete(object);
 49         }
 50     }
 51 
 52     public void update(T entity) {
 53         // TODO Auto-generated method stub
 54         getSession().update(entity);
 55     }
 56 
 57     public T getById(Long id) {
 58         // TODO Auto-generated method stub
 59         if(id != null){
 60             return (T)getSession().get(clazz, id);
 61         }else{
 62             return null;
 63         }
 64     }
 65 
 66     public List<T> getByIds(Long[] ids) {
 67         // TODO Auto-generated method stub\
 68         if(ids == null || ids.length == 0){
 69             return Collections.emptyList();
 70         }else{
 71             return getSession().createQuery("from "+
 72                     clazz.getSimpleName() + " where id in(:ids)").setParameterList("ids",ids).list();
 73         }
 74     }
 75     
 76     public List<T> find(String hql , String param)
 77     {
 78         // 創建查詢
 79         Query query = getSession()
 80             .createQuery(hql);
 81         // 為包含占位符的HQL語句設置參數
 82         
 83             query.setParameter("1",param);
 84         
 85         return (List<T>)query.list();
 86     }
 87 
 88     public List<T> findAll() {
 89         // TODO Auto-generated method stub
 90         return getSession().createQuery("from " + clazz.getSimpleName()).list();
 91     }
 92 
 93     public Long totalNum() {
 94         // TODO Auto-generated method stub
 95         return (Long)getSession().createQuery("select count(*) from " + clazz.getSimpleName()).uniqueResult();
 96     }
 97 
 98     public List<T> getPage(int pageNow, int pageSize) {
 99         // TODO Auto-generated method stub
100         return getSession().createQuery("from " + clazz.getSimpleName()).setFirstResult((pageNow - 1) * pageSize).setMaxResults(pageSize).list();
101     }
102     
103     /*@SuppressWarnings("unchecked")
104     public T get(Class<T> entityClazz , String id)
105     {
106         return (T)((SessionFactory) getSession()).getCurrentSession()
107             .get(entityClazz , id);
108     }*/
109 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM