hibernate 操作 模板基类设计

来源:互联网 发布:硬盘不能识别数据恢复 编辑:程序博客网 时间:2024/05/17 01:08


把hibernate   的操作集成在基类当中,可以减少很多的工作量


第一步: 操作接口
IGenericDao.java
      所用的集成操作 增删改查

Java代码 复制代码 收藏代码
  1. package com.ssh.common.dao;   
  2.   
  3. /**  
  4.  * 功能:hibernate  操作 模板基类设计  
  5.  *  
  6.  * @author programming  
  7.  * @version 1.0  2009-3-10下午04:35:34  
  8.  * @JDK 5   
  9.  */  
  10. import java.io.Serializable;   
  11. import java.util.Collection;   
  12. import java.util.List;   
  13.   
  14. import org.hibernate.LockMode;   
  15. import org.hibernate.criterion.DetachedCriteria;   
  16. import org.springframework.dao.DataAccessException;   
  17.   
  18. import com.ssh.common.util.PaginationSupport;   
  19.   
  20. public interface IGenericDao<T, ID extends Serializable> {   
  21.   
  22.     public T load(ID id) throws DataAccessException;   
  23.   
  24.     public T get(ID id) throws DataAccessException;   
  25.   
  26.     public boolean contains(T t) throws DataAccessException;   
  27.   
  28.     public void refresh(T t, LockMode lockMode) throws DataAccessException;   
  29.   
  30.     public void refresh(T t) throws DataAccessException;   
  31.   
  32.     public Serializable save(T t) throws DataAccessException;   
  33.   
  34.     public void saveOrUpdate(T t) throws DataAccessException;   
  35.   
  36.     public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException;   
  37.   
  38.     public void update(T t, LockMode lockMode) throws DataAccessException;   
  39.   
  40.     public void update(T t) throws DataAccessException;   
  41.   
  42.     public void delete(T t, LockMode lockMode) throws DataAccessException;   
  43.   
  44.     public void delete(T t) throws DataAccessException;   
  45.   
  46.     public void deleteAll(Collection<T> entities) throws DataAccessException;   
  47.   
  48.     public List<T> find(String queryString, Object value) throws DataAccessException;   
  49.   
  50.     public List<T> find(String queryString, Object[] values) throws DataAccessException;   
  51.   
  52.     public List<T> find(String queryString) throws DataAccessException;   
  53.   
  54.     public List<T> list() throws DataAccessException;   
  55.   
  56.     public List<T> findByNamedQuery(String queryName) throws DataAccessException;   
  57.   
  58.     public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException;   
  59.   
  60.     public List<T> findByNamedQuery(String queryName, Object[] values) throws DataAccessException;   
  61.   
  62.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,   
  63.             final int startIndex);   
  64.   
  65.     public PaginationSupport findPageByQuery(final String hql, final String countHql, final int pageSize,   
  66.             final int startIndex);   
  67.   
  68. }  




实现此接口的反射类
GenericDao.java

Java代码 复制代码 收藏代码
  1. package com.ssh.common.dao;   
  2.   
  3. /**  
  4.  * 功能:  
  5.  *  
  6.  * @author programming  
  7.  * @version 1.0  2009-3-10下午04:36:22  
  8.  * @JDK 5   
  9.  */  
  10. import java.io.Serializable;   
  11. import java.lang.reflect.ParameterizedType;   
  12. import java.sql.SQLException;   
  13. import java.util.Collection;   
  14. import java.util.List;   
  15.   
  16. import org.apache.commons.logging.Log;   
  17. import org.apache.commons.logging.LogFactory;   
  18. import org.hibernate.Criteria;   
  19. import org.hibernate.HibernateException;   
  20. import org.hibernate.LockMode;   
  21. import org.hibernate.Query;   
  22. import org.hibernate.Session;   
  23. import org.hibernate.criterion.DetachedCriteria;   
  24. import org.hibernate.criterion.Projections;   
  25. import org.springframework.dao.DataAccessException;   
  26. import org.springframework.orm.hibernate3.HibernateCallback;   
  27. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  28.   
  29. import com.ssh.common.util.PaginationSupport;   
  30.   
  31. @SuppressWarnings("unchecked")   
  32. public class GenericDao<T, ID extends Serializable> extends HibernateDaoSupport implements IGenericDao<T, ID> {   
  33.     private Log logger = LogFactory.getLog(getClass());   
  34.   
  35.     protected Class<T> entityClass;   
  36.   
  37.     public GenericDao() {   
  38.   
  39.     }   
  40.   
  41.     protected Class getEntityClass() {   
  42.         if (entityClass == null) {   
  43.             entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];   
  44.             logger.debug("T class = " + entityClass.getName());   
  45.         }   
  46.         return entityClass;   
  47.     }   
  48.   
  49.     public void saveOrUpdate(T t) throws DataAccessException {   
  50.         this.getHibernateTemplate().saveOrUpdate(t);   
  51.     }   
  52.   
  53.     public T load(ID id) throws DataAccessException {   
  54.         T load = (T) getHibernateTemplate().load(getEntityClass(), id);   
  55.         return load;   
  56.     }   
  57.   
  58.     public T get(ID id) throws DataAccessException {   
  59.         T load = (T) getHibernateTemplate().get(getEntityClass(), id);   
  60.         return load;   
  61.     }   
  62.   
  63.     public boolean contains(T t) throws DataAccessException {   
  64.         return getHibernateTemplate().contains(t);   
  65.     }   
  66.   
  67.     public void delete(T t, LockMode lockMode) throws DataAccessException {   
  68.         getHibernateTemplate().delete(t, lockMode);   
  69.     }   
  70.   
  71.     public void delete(T t) throws DataAccessException {   
  72.         getHibernateTemplate().delete(t);   
  73.     }   
  74.   
  75.     public void deleteAll(Collection<T> entities) throws DataAccessException {   
  76.         getHibernateTemplate().deleteAll(entities);   
  77.     }   
  78.   
  79.     public List<T> find(String queryString, Object value) throws DataAccessException {   
  80.         List<T> find = (List<T>) getHibernateTemplate().find(queryString, value);   
  81.         return find;   
  82.     }   
  83.   
  84.     public List<T> find(String queryString, Object[] values) throws DataAccessException {   
  85.         List<T> find = (List<T>) getHibernateTemplate().find(queryString, values);   
  86.         return find;   
  87.     }   
  88.   
  89.     public List<T> find(String queryString) throws DataAccessException {   
  90.         return (List<T>) getHibernateTemplate().find(queryString);   
  91.     }   
  92.   
  93.     public void refresh(T t, LockMode lockMode) throws DataAccessException {   
  94.         getHibernateTemplate().refresh(t, lockMode);   
  95.     }   
  96.   
  97.     public void refresh(T t) throws DataAccessException {   
  98.         getHibernateTemplate().refresh(t);   
  99.     }   
  100.   
  101.     public Serializable save(T t) throws DataAccessException {   
  102.         return getHibernateTemplate().save(t);   
  103.     }   
  104.   
  105.     public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException {   
  106.         getHibernateTemplate().saveOrUpdateAll(entities);   
  107.     }   
  108.   
  109.     public void update(T t, LockMode lockMode) throws DataAccessException {   
  110.         getHibernateTemplate().update(t, lockMode);   
  111.     }   
  112.   
  113.     public void update(T t) throws DataAccessException {   
  114.         getHibernateTemplate().update(t);   
  115.     }   
  116.   
  117.     public List<T> list() throws DataAccessException {   
  118.         return getHibernateTemplate().loadAll(getEntityClass());   
  119.   
  120.     }   
  121.   
  122.     public List<T> findByNamedQuery(String queryName) throws DataAccessException {   
  123.         return getHibernateTemplate().findByNamedQuery(queryName);   
  124.     }   
  125.   
  126.     public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException {   
  127.         return getHibernateTemplate().findByNamedQuery(queryName, value);   
  128.     }   
  129.   
  130.     public List<T> findByNamedQuery(String queryName, Object[] values) throws DataAccessException {   
  131.         return getHibernateTemplate().findByNamedQuery(queryName, values);   
  132.     }   
  133.   
  134.     public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,   
  135.             final int startIndex) {   
  136.         return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
  137.             public Object doInHibernate(Session session) throws HibernateException {   
  138.                 Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
  139.                 int totalCount = ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();   
  140.                 criteria.setProjection(null);   
  141.                 List items = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();   
  142.                 PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);   
  143.                 return ps;   
  144.             }   
  145.         }, true);   
  146.     }   
  147.   
  148.     public PaginationSupport findPageByQuery(final String hql, final String countHql, final int pageSize,   
  149.             final int startIndex) {   
  150.         return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {   
  151.             public Object doInHibernate(Session session) throws HibernateException, SQLException {   
  152.                 int totalCount = ((Integer) session.createQuery(countHql).iterate().next()).intValue();   
  153.                 Query query = session.createQuery(hql);   
  154.                 query.setFirstResult(startIndex);   
  155.                 query.setMaxResults(pageSize);   
  156.                 List items = query.list();   
  157.                 PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);   
  158.                 return ps;   
  159.   
  160.             }   
  161.         }, true);   
  162.     }   
  163.   
  164. }  


由此基类可以节省很多的工作

Java代码 复制代码 收藏代码
  1. public class UserDao extends GenericDao<User, Integer> implements IUserDao {}   
  2. public class BookDao extends GenericDao<Book, Integer> implements IBookDao {}  



以此类推

更容易容缩代码

  欢迎大家批阅,

    有更好的设计发出来看一下,我们一起学习,一起进步