利用泛型减少Dao方法的数量

  1. package com.oa.dao;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.*;  
  5.   
  6. /** 
  7.  * * 
  8.  *  
  9.  * @param <T> 
  10.  *            泛 型,指实体类  type 
  11.  * @param <PK> 
  12.  *            泛 型,指实体类主键的数据类型,如Integer,Long 
  13.  */  
  14. public interface GenericDao<T, PK> {  
  15.       
  16.     /**   
  17.      * 保 存指定实体类   
  18.      *    
  19.      * @param entityobj   
  20.      *            实 体类   
  21.      */   
  22.     public  void save(T entity);  
  23.       
  24.       
  25.       /**   
  26.      * 删 除指定实体   
  27.      *    
  28.      * @param entityobj   
  29.      *            实 体类   
  30.      */    
  31.     public void delete(T entity);  
  32.       
  33.       
  34.      /** * 
  35.      * 删 除实体 
  36.      * @param entityClass 实体类名 
  37.      * @param id 实 体的ID 
  38.      */  
  39.     public void deleteById(Class<T> entityClass,PK id);  
  40.       
  41.       
  42.     /**   
  43.     * 更 新或保存指定实体   
  44.     *    
  45.     * @param entity 实 体类   
  46.     */   
  47.     public void saveorupdate(T entity);  
  48.       
  49.       
  50.      /** * 
  51.      * 更 新实体 
  52.      * 可用于添加、修改、删除操作 
  53.      * @param hql 更 新的HQL语句 
  54.      * @param params 参数,可有项目或 多项目,代替Hql中的"?"号 
  55.      */  
  56.     public void update(final String hql,final Object[] params);  
  57.       
  58.       
  59.   
  60.     /**   
  61.      * 模 糊查询指定条件对象集合 <br>   
  62.      * 用法:可以 实例化一个空的T对象,需要查询某个字段,就set该字段的条件然后调用本方法<br>   
  63.      * 缺 点:目前测试貌似只能支持String的模糊查询,虽然有办法重写,但没必要,其他用HQL<br>   
  64.      *    
  65.      * @param entity   
  66.      *            条 件实体   
  67.      * @return 结合   
  68.      */    
  69.     public List<T> findByExample(T entity);  
  70.       
  71.       
  72.     /**   
  73.      * 获 取所有实体集合   
  74.      *    
  75.      * @param entityClass   
  76.      *            实 体   
  77.      * @return 集合   
  78.      */    
  79.     public List<T> findAll(Class<T> entityClass);  
  80.       
  81.     public List<T> findAll(Class<T> entityClass,String hql,Object[] params,int start, int limit);  
  82.     /**   
  83.      * 查 找指定PK实体类对象   
  84.      *    
  85.      * @param entityClass   
  86.      *            实 体Class   
  87.      * @param id   
  88.      *            实 体PK   
  89.      * @return 实体对象   
  90.      */     
  91.     public T findById(Class<T> entityClass, PK id);  
  92.       
  93.     /** * 
  94.      * 按 HQL条件查询列表 
  95.      * @param hql 查询语句,支持连接 查询和多条件查询 
  96.      * @param params 参数数组,代替 hql中的"?"号 
  97.      * @return 结果集List 
  98.      */  
  99.      
  100.     public List<T> findByHql(String hql,Object[]  params);  
  101.       
  102.     /**   
  103.      * 查 找指定属性的实体集合   
  104.      *    
  105.      * @param entityClass   
  106.      *            实 体   
  107.      * @param propertyName   
  108.      *            属 性名   
  109.      * @param value   
  110.      *            条 件   
  111.      * @return 实体集合   
  112.      */    
  113.     public List<T> findByProperty(Class<T> entityClass, String propertyName,Object value);  
  114.       
  115.       
  116.     /**   
  117.      * 查 询指定HQL语句的分页数据集合   
  118.      *    
  119.      * @param hsql   
  120.      *            HQL 语句   
  121.      * @param start   
  122.      *            开 始记录号   
  123.      * @param limit   
  124.      *            最 大记录号   
  125.      * @return 分页数据集合   
  126.      * @throws Exception   
  127.      *             抛 出异常   
  128.      */    
  129.     public List<T> findByPage(Class<T> entityClass,int start,int limit) ;  
  130.       
  131.       
  132.       
  133.     /** 
  134.      * 获 得总记录数 
  135.      */  
  136.     public T getTotalCount(Class<T> entityClass);  
  137.       
  138.     public T getPageCount(String hql,Object[] params);  
  139.   
  140. package com.oa.dao.impl;  
  141.   
  142. import java.io.Serializable;  
  143. import java.util.List;  
  144.   
  145. import org.hibernate.Query;  
  146. import org.springframework.stereotype.Repository;  
  147.   
  148. import com.oa.dao.GenericDao;  
  149. import com.oa.dao.MyHibernateDaoSupport;  
  150. @SuppressWarnings("unchecked")  
  151. @Repository("genericDao")   // 声明此类为数据持久层的类  
  152. public class GenericDaoImpl<T, PK extends Serializable> extends  
  153.                                     MyHibernateDaoSupport implements GenericDao<T, PK> {  
  154.   
  155.     public void delete(T entity) {  
  156.         super.getHibernateTemplate().delete(entity);  
  157.     }  
  158.   
  159.   
  160.     public void deleteById(Class entityClass, PK id) {  
  161.         super.getHibernateTemplate().delete(findById(entityClass, id));  
  162.   
  163.     }  
  164.       
  165.     public void save(T entity) {  
  166.         super.getHibernateTemplate().save(entity);  
  167.   
  168.     }  
  169.   
  170.     public void saveorupdate(T entity) {  
  171.         super.getHibernateTemplate().saveOrUpdate(entity);  
  172.   
  173.     }  
  174.   
  175.     public void update(String hql, Object[] params) {  
  176.              Query query = super.getSession().createQuery(hql);  
  177.                 for(int i=0; i<params.length; i++){  
  178.                     query.setParameter(i, params[i]);  
  179.                 }  
  180.                 query.executeUpdate();  
  181.      }  
  182.       
  183.   
  184.     public List<T> findAll(Class entityClass) {  
  185.           
  186.         return super.getHibernateTemplate().loadAll(entityClass);  
  187.     }  
  188.   
  189.     public List<T> findAll(Class entityClass, String hql, Object[] params,int start, int limit) {  
  190.         Query query = super.getSession().createQuery(hql);  
  191.         if(params!=null&&params.length>0){  
  192.             for(int i = 0;i<params.length;i++){  
  193.                 query.setParameter(i, params[i]);  
  194.             }  
  195.         }  
  196.         if(start!=0&&limit!=0){  
  197.             query.setFirstResult(start).setMaxResults(limit);  
  198.         }  
  199.         return query.list();  
  200.     }  
  201.   
  202.     public List<T> findByExample(T entity) {  
  203.         return super.getHibernateTemplate().findByExample(entity);  
  204.     }  
  205.   
  206.     public List<T> findByHql(String hql, Object[] params) {  
  207.         Query query = super.getSession().createQuery(hql);  
  208.         if(null!= params && params.length>0){  
  209.             for(int i = 0; i<params.length;i++){  
  210.                 query.setParameter(i, params[i]);  
  211.             }  
  212.         }  
  213.         return query.list();  
  214.     }  
  215.   
  216.     public T findById(Class entityClass, PK id) {  
  217.         return (T)super.getHibernateTemplate().get(entityClass, id);  
  218.     }  
  219.       
  220.     public List<T> findByProperty(Class entityClass, String propertyName,Object value) {  
  221.         String queryString = "from "+entityClass.getName()+ " as model where model." + propertyName + "=?";     
  222.         return super.getHibernateTemplate().find(queryString, value);  
  223.     }  
  224.   
  225.   
  226.       
  227.     // 分页使用  
  228.     public List<T> findByPage(Class<T> entityClass,int start,int limit) {  
  229.           Query query=super.getSession().createQuery("select o from "+entityClass.getName()+" o");  
  230.           query.setFirstResult(start).setMaxResults(limit);  
  231.         return query.list();  
  232.     }  
  233.   
  234.       
  235.     public T getTotalCount(Class entityClass) {  
  236.           
  237.         return (T)super.getSession().createQuery("select count(o) from "+entityClass.getName()+" o").uniqueResult();  
  238.     }  
  239.   
  240.     public T getPageCount(String hql, Object[] params) {  
  241.         Query query = super.getSession().createQuery(hql);  
  242.         if(null!= params && params.length>0){  
  243.             for(int i = 0; i<params.length;i++){  
  244.                 query.setParameter(i, params[i]);  
  245.             }  
  246.         }  
  247.         return (T)query.list();  
  248.     }  
  249.   
  250.       
  251.   

至此 泛型就告一个段落。

 


接下来日子就好过了。

我们不是有user  news   等等一系列的curd管理。


以User为例子;
定义一个user的接口,
UserDao.Java

Java代码 <embed height="50" width="400" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="elmtId=28507_1245983629038&paramStr=++src%3D%22http%3A%2F%2Fwww.javaeye.com%2Fjavascripts%2Fsyntaxhighlighter%2Fclipboard_new.swf%22++flashvars%3D%22clipboard%3Dpackage%2520com.oa.dao%253B%250A%250Aimport%2520com.oa.User%253B%250A%250Apublic%2520interface%2520UserDao%2520extends%2520GenericDao%253CUser%252C%2520Integer%253E%2520%257B%250A%250Apublic%2520%2520%2520int%2520%2520%2520login%25EF%25BC%2588User%2520user%25EF%25BC%2589%253B%250A%252F%252F%25E5%2585%25B6%25E4%25BB%2596%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%25E7%259A%2584%250A%257D%250A%250A%250A%25E7%2584%25B6%25E5%2590%258E%25E5%25B0%25B1%25E6%2598%25AF%25E5%25AE%259E%25E7%258E%25B0%25E5%25AE%2583%2520UserDaoImpl%250A%250Apackage%2520com.oa.dao.impl%253B%250A%250A%250A%250Aimport%2520com.oa.User%253B%250Aimport%2520com.oa.dao.UserDao%253B%250A%250Apublic%2520class%2520UserDaoImpl%2520extends%2520GenericDaoImpl%253CUser%252C%2520Integer%253E%2520implements%2520UserDao%2520%257B%250A%250A%2509public%2520%2520int%2520%2520login%25EF%25BC%2588User%2520%2520user%25EF%25BC%2589%257B%250A%252F%252F%25E7%2599%25BB%25E9%2599%2586%25E5%2588%25A4%25E6%2596%25AD%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%250A%250Areturn%2520%2520%2520XX%253B%250A%257D%253B%250A%250A%250A%252F%252F%25E5%2585%25B6%25E4%25BB%2596%25E7%259A%2584%25E6%2596%25B9%25E6%25B3%2595%25E7%259A%2584%25E5%25AE%259E%25E7%258E%25B0%250A%250A%250A%257D%250A%250A%2520%22+quality%3D%22high%22+type%3D%22application%2Fx-shockwave-flash%22+width%3D%2214%22+height%3D%2215%22" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" quality="high" src="http://ecjtu05.blog.sohu.com/flash/embedloader.swf" id="28507_1245983629038">
  1. package com.oa.dao;  
  2.   
  3. import com.oa.User;  
  4.   
  5. public interface UserDao extends GenericDao<User, Integer> {  
  6.   
  7. public   int   login(User user);  
  8. // 其他的方法的  
  9. }  
  10.   
  11.   
  12. 然 后就是实现它 UserDaoImpl  
  13.   
  14. package com.oa.dao.impl;  
  15.   
  16.   
  17.   
  18. import com.oa.User;  
  19. import com.oa.dao.UserDao;  
  20.   
  21. public class UserDaoImpl extends GenericDaoImpl<User, Integer> implements UserDao {  
  22.   
  23.     public  int  login(User  user) {  
  24. //登陆判断的方法  
  25.   
  26. return   XX;  
  27. };  
  28.   
  29.   
  30. // 其他的方法的实现  
  31.   
  32.   

posted on 2010-08-17 09:20  基斯盐  阅读(835)  评论(0编辑  收藏  举报

导航