一种好的持久层开发方法——建立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 }
原文地址:https://www.cnblogs.com/godlei/p/5565683.html