学习使用泛型dao

(1)泛型接口GenericDao


public interface GenericDao <T, ID extends Serializable>{   
 
 public T findById(ID id);
 
    T findById(ID id, boolean lock);   
 
    public T load(ID id);
    
    List findAll();
 
    List findByExample(T exampleInstance);   
  
    T save(T entity);   
  
    void update(T entity);
    
    void delete(T entity);   
} 


(2)泛型接口实现GenericDaoImpl


public abstract class GenericDaoImpl <T, ID extends Serializable>  
extends HibernateDaoSupport implements GenericDao <T, ID>{   


 private Class persistentClass;   

 public GenericDaoImpl() {   
 this.persistentClass = (Class) ((ParameterizedType) getClass()   
         .getGenericSuperclass()).getActualTypeArguments()[0];   
 }   


 public Class getPersistentClass() {   
 return persistentClass;
 }   


 @SuppressWarnings("unchecked")
 public T findById(ID id) {
  return (T) getHibernateTemplate().get(getPersistentClass(), id);  
 }   


 @SuppressWarnings("unchecked")
 public T load(ID id){
  T t = (T)getHibernateTemplate().load(getPersistentClass(), id);
  return t;
 }
 
 @SuppressWarnings("unchecked")   
 public T findById(ID id, boolean lock) {   
 T entity;
 if (lock)   
     entity = (T) getHibernateTemplate().load(getPersistentClass(), id,   
             LockMode.UPGRADE);
 else  
     entity = findById(id);   
 
 return entity;   
 }   


 public List findAll() {   
  return findByCriteria();   
 }   


 @SuppressWarnings("unchecked")   
 public List findByExample(T exampleInstance) {   
 DetachedCriteria detachedCrit = DetachedCriteria   
         .forClass(getPersistentClass());   
 Example example = Example.create(exampleInstance);   
 detachedCrit.add(example);   
 return getHibernateTemplate().findByCriteria(detachedCrit);   
 }   


 @SuppressWarnings("unchecked")   
 public List findByExample(T exampleInstance, String[] excludeProperty) {   
 DetachedCriteria detachedCrit = DetachedCriteria   
         .forClass(getPersistentClass());   
 Example example = Example.create(exampleInstance);   
 for (String exclude : excludeProperty) {   
     example.excludeProperty(exclude);   
 }   


 detachedCrit.add(example);   
 return getHibernateTemplate().findByCriteria(detachedCrit);   
 }   


 public T save(T entity) {   
  getHibernateTemplate().saveOrUpdate(entity);   
 return entity;   
 }   


 public void update(T entity){
  getHibernateTemplate().update(entity);   
 }
 
 public void delete(T entity) {   
 getHibernateTemplate().delete(entity);   
 }   


 @SuppressWarnings("unchecked")   
 protected List findByCriteria(Criterion... criterion) {   
 DetachedCriteria detachedCrit = DetachedCriteria   
         .forClass(getPersistentClass());   
 for (Criterion c : criterion) {   
     detachedCrit.add(c); 
 }   
 return getHibernateTemplate().findByCriteria(detachedCrit);   
 }   
}  


(3)CustomersDao


public interface CustomersDao extends GenericDao <Customers,Integer>{

}


(4)CustomersDaoImpl


public class CustomersDaoImpl extends GenericDaoImpl <Customers,Integer> implements CustomersDao{

}

你可能感兴趣的:(DAO,C++,c,C#)