Hibernate_CRUD组件

来源:互联网 发布:linux查看php日志文件 编辑:程序博客网 时间:2024/06/14 11:11

本组件继承了HibernateDaoSupport,并完成对HibernateDaoSupport进行二次封装。提取平时开发常用的底层操作方法,并根据个

人习惯,定义自己的编码规范。根据sun官方的最新开发规范,使用了jdk的新特性——泛型。所有的操作对象以泛型指定。

定义接口规范:IBaseDao.java

[java] view plaincopyprint?
  1. package com.hoo.dao;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. import org.hibernate.Session;  
  7. import org.hibernate.criterion.DetachedCriteria;  
  8. import org.springframework.orm.hibernate3.HibernateTemplate;  
  9.   
  10. import com.hoo.entity.Page;  
  11.   
  12. /*** 
  13.  * <b>function:</b> 增删改查组件规范接口 
  14.  * @project NetWorkService 
  15.  * @package com.hoo.dao  
  16.  * @fileName IBaseDao.java 
  17.  * @createDate 2010-8-2 下午05:28:03 
  18.  * @author hoojo 
  19.  * @email hoojo_@126.com 
  20.  * @blog http://blog.csdn.net/IBM_hoojo 
  21.  */  
  22. public interface IBaseDao {  
  23.       
  24.     /** 
  25.      * <b>function:</b> 增加一个entity对象,返回是否添加成功 
  26.      * @createDate 2010-8-2 下午05:28:38 
  27.      * @author hoojo 
  28.      * @param <T> 对象类型 
  29.      * @param entity 对象 
  30.      * @return boolean true/false 
  31.      * @throws Exception 
  32.      */  
  33.     public <T> boolean add(T entity) throws Exception;  
  34.       
  35.     /** 
  36.      * <b>function:</b> 添加一个entity对象,返回添加对象的Integer类型的主键 
  37.      * @createDate 2010-8-2 下午05:29:39 
  38.      * @author hoojo 
  39.      * @param <T> 对象类型 
  40.      * @param entity 将要添加的对象 
  41.      * @return Integer 返回主键 
  42.      * @throws Exception 
  43.      */  
  44.     public <T> Integer addAndGetId4Integer(T entity) throws Exception;  
  45.       
  46.     /** 
  47.      * <b>function:</b> 添加一个对象并且返回该对象的String类型的主键 
  48.      * @createDate 2010-8-2 下午05:31:32 
  49.      * @author hoojo 
  50.      * @param <T> 对象类型 
  51.      * @param entity 将要添加的对象 
  52.      * @return String 返回的主键 
  53.      * @throws Exception 
  54.      */  
  55.     public <T> String addAndGetId4String(T entity) throws Exception;  
  56.       
  57.     /** 
  58.      * <b>function:</b> 传入hql语句执行 
  59.      * @createDate 2010-8-2 下午04:42:26 
  60.      * @author hoojo 
  61.      * @param hql String hql语句 
  62.      * @return int 影响行数 
  63.      * @throws Exception 
  64.      */  
  65.     public int executeByHql(String hql) throws Exception;  
  66.       
  67.     /** 
  68.      * <b>function:</b> 传入hql语句执行查询,返回list集合 
  69.      * @createDate 2010-8-3 上午10:00:34 
  70.      * @author hoojo 
  71.      * @param hql 查询的hql语句 
  72.      * @return List集合 
  73.      * @throws Exception 
  74.      */  
  75.     public <T> List<T> findByHql(String hql) throws Exception;  
  76.       
  77.     /** 
  78.      * <b>function:</b> 执行原生态的sql语句,添加、删除、修改语句 
  79.      * @createDate 2010-8-2 下午05:33:42 
  80.      * @author hoojo 
  81.      * @param sql 将要执行的sql语句 
  82.      * @return int 
  83.      * @throws Exception 
  84.      */  
  85.     public int executeBySql(String sql) throws Exception;  
  86.       
  87.     /** 
  88.      * <b>function:</b> 传入sql语句执行查询,返回list集合 
  89.      * @createDate 2010-8-3 上午10:00:34 
  90.      * @author hoojo 
  91.      * @param sql 查询的sql语句 
  92.      * @return List集合 
  93.      * @throws Exception 
  94.      */  
  95.     public <T> List<T> findBySql(String sql) throws Exception;  
  96.       
  97.     /** 
  98.      * <b>function:</b> 修改entity对象,返回是否修改成功 
  99.      * @createDate 2010-8-2 下午05:35:47 
  100.      * @author hoojo 
  101.      * @param <T> 对象类型 
  102.      * @param entity 将要修改的对象 
  103.      * @return boolean true/false 是否修改成功 
  104.      * @throws Exception 
  105.      */  
  106.     public <T> boolean edit(T entity) throws Exception;  
  107.       
  108.     /** 
  109.      * <b>function:</b> 传入hql语句执行修改,返回是否修改成功 
  110.      * @createDate 2010-8-2 下午05:36:31 
  111.      * @author hoojo 
  112.      * @param hql 查询的hql语句 
  113.      * @return boolean true/false 返回是否修改成功 
  114.      * @throws Exception 
  115.      */  
  116.     public boolean edit(String hql) throws Exception;  
  117.       
  118.     /** 
  119.      * <b>function:</b> 执行修改的hql语句,返回修改的行数 
  120.      * @createDate 2010-8-2 下午05:38:58 
  121.      * @author hoojo 
  122.      * @param hql 修改语句 
  123.      * @return int 返回修改的行数 
  124.      * @throws Exception 
  125.      */  
  126.     public int editByHql(String hql) throws Exception;  
  127.       
  128.     /** 
  129.      * <b>function:</b> 传入一个将要删除的entity对象,返回删除是否成功 
  130.      * @createDate 2010-8-2 下午05:42:02 
  131.      * @author hoojo 
  132.      * @param <T> 传入对象类型 
  133.      * @param entity 将要传入的对象 
  134.      * @return boolean true/false 
  135.      * @throws Exception 
  136.      */  
  137.     public <T> boolean remove(T entity) throws Exception;  
  138.       
  139.     /** 
  140.      * <b>function:</b> 传入一个entity对象Class和String型主键,返回该对象 
  141.      * @createDate 2010-8-2 下午05:44:53 
  142.      * @author hoojo 
  143.      * @param <T> 返回、传入对象类型 
  144.      * @param c 对象Class 
  145.      * @param id 主键 
  146.      * @return T 返回传入类型对象 
  147.      * @throws Exception 
  148.      */  
  149.     public <T> T getById(Class<T> c, String id) throws Exception;  
  150.       
  151.     /** 
  152.      * <b>function:</b> 传入一个entity对象Class和Integer类型主键,返回该对象 
  153.      * @createDate 2010-8-2 下午05:47:20 
  154.      * @author hoojo 
  155.      * @param <T> 返回、传入对象类型 
  156.      * @param c 对象Class 
  157.      * @param id 主键 
  158.      * @return T 返回该类型的对象 
  159.      * @throws Exception 
  160.      */  
  161.     public <T> T getById(Class<T> c, Integer id) throws Exception;  
  162.       
  163.     /** 
  164.      * <b>function:</b> 传入一个entity对象Class和Serializable类型主键,返回该对象 
  165.      * @createDate 2010-8-2 下午05:48:36 
  166.      * @author hoojo 
  167.      * @param <T> 返回、传入对象类型 
  168.      * @param c 对象Class 
  169.      * @param id 主键 
  170.      * @return T 返回该类型的对象 
  171.      * @throws Exception 
  172.      */  
  173.     public <T> T get(Class<T> c, Serializable id) throws Exception;  
  174.       
  175.     /** 
  176.      * <b>function:</b> 传入hql语句,查询对象 
  177.      * @createDate 2010-8-2 下午05:49:31 
  178.      * @author hoojo 
  179.      * @param <T> 返回对象类型 
  180.      * @param hql 查询的hql语句 
  181.      * @return 对象T 
  182.      * @throws Exception 
  183.      */  
  184.     public <T> T get(String hql) throws Exception;  
  185.       
  186.     /** 
  187.      * <b>function:</b> 通过hql语句查询List集合 
  188.      * @createDate 2010-8-2 下午05:51:05 
  189.      * @author hoojo 
  190.      * @param hql 查询hql语句 
  191.      * @return List<?> 
  192.      * @throws Exception 
  193.      */  
  194.     public <T> List<T> getList(String hql) throws Exception;  
  195.       
  196.     /** 
  197.      * <b>function:</b> 传入删除的hql语句,删除记录 
  198.      * @createDate 2010-8-3 上午09:53:49 
  199.      * @author hoojo 
  200.      * @param hql 将要被执行删除的hql语句 
  201.      * @return 是否删除成功 
  202.      * @throws Exception 
  203.      */  
  204.     public boolean remove(String hql) throws Exception;  
  205.       
  206.     /** 
  207.      * <b>function:</b> 动态查询 
  208.      * @createDate 2010-8-3 上午10:53:37 
  209.      * @author hoojo 
  210.      * @param <T> 查询类的类型 
  211.      * @param c 动态查询组合对象 
  212.      * @return list集合 
  213.      * @throws Exception 
  214.      */  
  215.     public <T> List<T> getList(Class<T> c) throws Exception;  
  216.       
  217.       
  218.     /** 
  219.      * <b>function:</b> 传入hql查询语句和object数组类型的参数,返回查询list集合 
  220.      * @createDate 2010-8-2 下午05:52:36 
  221.      * @author hoojo 
  222.      * @param hql 查询的hql语句 
  223.      * @param obj 查询参数 
  224.      * @return 返回list集合 
  225.      * @throws Exception 
  226.      */  
  227.     public <T> List<T> getList(String hql, Object[] obj) throws Exception;  
  228.       
  229.       
  230.     /** 
  231.      * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; 
  232.      * list集合保存总记录调试和记录结果 
  233.      * @createDate 2010-8-2 下午05:54:01 
  234.      * @author hoojo 
  235.      * @param queryHql 查询记录hql语句 
  236.      * @param queryCountHql 查询记录条数hql语句 
  237.      * @param firstResult 当前查询页 
  238.      * @param maxResult 每页显示多少条 
  239.      * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 
  240.      * @throws Exception 
  241.      */  
  242.     public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception;  
  243.       
  244.     /** 
  245.      * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; 
  246.      * @createDate 2010-8-3 上午11:16:59 
  247.      * @author hoojo 
  248.      * @param queryHql list集合结果查询 
  249.      * @param queryCountHql 总记录调试查询 
  250.      * @param page 分页对象 
  251.      * @throws Exception 
  252.      */  
  253.     public <T> void showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception;  
  254.       
  255.     /** 
  256.      * <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 
  257.      * @createDate 2010-8-3 上午11:04:39 
  258.      * @author hoojo 
  259.      * @param queryCountHql hql查询count语句总条数 
  260.      * @param cResult  DetachedCriteria 动态查询条件 
  261.      * @param firstResult 起始 
  262.      * @param maxResult 最大页数 
  263.      * @return List<?> 查询集合 
  264.      * @throws Exception 
  265.      */  
  266.     public List<?> showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception;  
  267.       
  268.     /** 
  269.      * <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity 
  270.      * @createDate 2010-8-3 上午11:14:30 
  271.      * @author hoojo 
  272.      * @param queryCountHql 查询count语句 
  273.      * @param cResult DetachedCriteria 动态查询组合类 
  274.      * @param page Page分页实体类 
  275.      * @throws Exception 
  276.      */  
  277.     public <T> void showPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception;  
  278.       
  279.     /** 
  280.      * <b>function:</b> 传入查询条件DetachedCriteria进行查询 
  281.      * @createDate 2010-8-3 上午11:55:28 
  282.      * @author hoojo 
  283.      * @param <T> 类型 
  284.      * @param dc DetachedCriteria动态条件查询 
  285.      * @return List 
  286.      * @throws Exception 
  287.      */  
  288.     public <T> List<T> find(DetachedCriteria dc) throws Exception;  
  289.       
  290.     /** 
  291.      * <b>function:</b> 暴露基类session供用户使用 
  292.      * @createDate 2010-8-3 上午11:59:54 
  293.      * @author hoojo 
  294.      * @return Session 
  295.      */  
  296.     public Session session();  
  297.       
  298.     /** 
  299.      * <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 
  300.      * @createDate 2010-8-3 上午11:58:51 
  301.      * @author hoojo 
  302.      * @return HibernateTemplate 
  303.      */  
  304.     public HibernateTemplate getTemplate();  
  305. }  

 

下面是实现IBaseDao接口的代码BaseDaoImpl.java:

[java] view plaincopyprint?
  1. package com.hoo.dao.impl;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.hibernate.Session;  
  8. import org.hibernate.criterion.DetachedCriteria;  
  9. import org.springframework.orm.hibernate3.HibernateTemplate;  
  10. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  11.   
  12. import com.hoo.dao.IBaseDao;  
  13. import com.hoo.entity.Page;  
  14.   
  15. /** 
  16.  * <b>function:</b> 删除改查组件实现类 
  17.  * @project NetWorkService 
  18.  * @package com.hhh.dao  
  19.  * @fileName BaseDAOImpl.java 
  20.  * @createDate 2010-8-2 下午05:58:45 
  21.  * @author hoojo 
  22.  * @email hoojo_@126.com 
  23.  * @blog http://blog.csdn.net/IBM_hoojo 
  24.  */  
  25. @SuppressWarnings("unchecked")  
  26. public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao {  
  27.       
  28.     /** 
  29.      * <b>function:</b> 增加一个entity对象,返回是否添加成功 
  30.      * @createDate 2010-8-2 下午05:28:38 
  31.      * @author hoojo 
  32.      * @param <T> 对象类型 
  33.      * @param entity 对象 
  34.      * @return boolean true/false 
  35.      * @throws Exception 
  36.      */  
  37.     public <T> boolean add(T entity) throws Exception {  
  38.         boolean bo = false;  
  39.         try {  
  40.             Serializable io = this.getHibernateTemplate().save(entity);  
  41.             if (io != null) {  
  42.                 bo = true;  
  43.             }  
  44.         } catch (Exception e) {  
  45.             bo = false;  
  46.             throw new RuntimeException(e);  
  47.         }  
  48.         return bo;  
  49.     }  
  50.       
  51.     /** 
  52.      * <b>function:</b> 添加一个entity对象,返回添加对象的Integer类型的主键 
  53.      * @createDate 2010-8-2 下午05:29:39 
  54.      * @author hoojo 
  55.      * @param <T> 对象类型 
  56.      * @param entity 将要添加的对象 
  57.      * @return Integer 返回主键 
  58.      * @throws Exception 
  59.      */  
  60.     public <T> Integer addAndGetId4Integer(T entity) throws Exception {  
  61.         Integer id = null;  
  62.         try {  
  63.             id = (Integer) this.getHibernateTemplate().save(entity);  
  64.         } catch (Exception e) {  
  65.             throw new RuntimeException(e);  
  66.         }  
  67.         return id;  
  68.     }  
  69.       
  70.     /** 
  71.      * <b>function:</b> 添加一个对象并且返回该对象的String类型的主键 
  72.      * @createDate 2010-8-2 下午05:31:32 
  73.      * @author hoojo 
  74.      * @param <T> 对象类型 
  75.      * @param entity 将要添加的对象 
  76.      * @return String 返回的主键 
  77.      * @throws Exception 
  78.      */  
  79.     public <T> String addAndGetId4String(T entity) throws Exception {  
  80.         String id = null;  
  81.         try {  
  82.             id = (String) this.getHibernateTemplate().save(entity);  
  83.         } catch (Exception e) {  
  84.             throw new RuntimeException(e);  
  85.         }  
  86.         return id;  
  87.     }  
  88.       
  89.     /** 
  90.      * <b>function:</b> 修改entity对象,返回是否修改成功 
  91.      * @createDate 2010-8-2 下午05:35:47 
  92.      * @author hoojo 
  93.      * @param <T> 对象类型 
  94.      * @param entity 将要修改的对象 
  95.      * @return boolean true/false 是否修改成功 
  96.      * @throws Exception 
  97.      */  
  98.     public <T> boolean edit(T entity) throws Exception {  
  99.         boolean bo = false;  
  100.         try {  
  101.             this.getHibernateTemplate().update(entity);  
  102.             bo = true;  
  103.         } catch (Exception e) {  
  104.             bo = false;  
  105.             throw new RuntimeException(e);  
  106.         }  
  107.         return bo;  
  108.     }  
  109.       
  110.     /** 
  111.      * <b>function:</b> 传入hql语句执行修改,返回是否修改成功 
  112.      * @createDate 2010-8-2 下午05:36:31 
  113.      * @author hoojo 
  114.      * @param hql 查询的hql语句 
  115.      * @return boolean true/false 返回是否修改成功 
  116.      * @throws Exception 
  117.      */  
  118.     public boolean edit(String hql) throws Exception {  
  119.         boolean bo = false;  
  120.         try {  
  121.             int count = this.getHibernateTemplate().bulkUpdate(hql);  
  122.             bo = count > 0 ? true : false;  
  123.         } catch (Exception e) {  
  124.             bo = false;  
  125.             throw new RuntimeException(e);  
  126.         }  
  127.         return bo;  
  128.     }  
  129.       
  130.     /** 
  131.      * <b>function:</b> 执行修改的hql语句,返回修改的行数 
  132.      * @createDate 2010-8-2 下午05:38:58 
  133.      * @author hoojo 
  134.      * @param hql 修改语句 
  135.      * @return int 返回修改的行数 
  136.      * @throws Exception 
  137.      */  
  138.     public int editByHql(String hql) throws Exception {  
  139.         int count = 0;  
  140.         try {  
  141.             count = this.getHibernateTemplate().bulkUpdate(hql);              
  142.         } catch (Exception e) {  
  143.             throw new RuntimeException(e);  
  144.         }  
  145.         return count;  
  146.     }  
  147.       
  148.     /** 
  149.      * <b>function:</b> 传入hql语句执行 
  150.      * @createDate 2010-8-2 下午04:42:26 
  151.      * @author hoojo 
  152.      * @param hql String hql语句 
  153.      * @return int 影响行数 
  154.      * @throws Exception 
  155.      */  
  156.     public int executeByHql(String hql) throws Exception {  
  157.         try {  
  158.             return this.getHibernateTemplate().bulkUpdate(hql);           
  159.         } catch (Exception e) {  
  160.             throw new RuntimeException(e);  
  161.         }  
  162.     }  
  163.       
  164.     /** 
  165.      * <b>function:</b> 传入hql语句执行查询,返回list集合 
  166.      * @createDate 2010-8-3 上午10:00:34 
  167.      * @author hoojo 
  168.      * @param hql 查询的hql语句 
  169.      * @return List集合 
  170.      * @throws Exception 
  171.      */  
  172.     public <T> List<T> findByHql(String hql) throws Exception {  
  173.         List list = null;  
  174.         try {  
  175.             list = (List<T>) this.getHibernateTemplate().find(hql);  
  176.         } catch (Exception e) {  
  177.             throw new RuntimeException(e);  
  178.         }  
  179.         return list;  
  180.     }  
  181.       
  182.     /** 
  183.      * <b>function:</b> 执行原生态的sql语句,添加、删除、修改语句 
  184.      * @createDate 2010-8-2 下午05:33:42 
  185.      * @author hoojo 
  186.      * @param sql 将要执行的sql语句 
  187.      * @return int 
  188.      * @throws Exception 
  189.      */  
  190.     public int executeBySql(String sql) throws Exception {  
  191.         try {  
  192.             return this.getSession().createSQLQuery(sql).executeUpdate();             
  193.         } catch (Exception e) {  
  194.             throw new RuntimeException(e);  
  195.         }  
  196.     }  
  197.       
  198.     /** 
  199.      * <b>function:</b> 传入sql语句执行查询,返回list集合 
  200.      * @createDate 2010-8-3 上午10:00:34 
  201.      * @author hoojo 
  202.      * @param sql 查询的sql语句 
  203.      * @return List集合 
  204.      * @throws Exception 
  205.      */  
  206.     public <T> List<T> findBySql(String sql) throws Exception {  
  207.         List list = null;  
  208.         try {  
  209.             list = (List<T>) this.getSession().createSQLQuery(sql).list();  
  210.         } catch (Exception e) {  
  211.             throw new RuntimeException(e);  
  212.         }  
  213.         return list;  
  214.     }  
  215.       
  216.     /** 
  217.      * <b>function:</b> 传入一个entity对象Class和Serializable类型主键,返回该对象 
  218.      * @createDate 2010-8-2 下午05:48:36 
  219.      * @author hoojo 
  220.      * @param <T> 返回、传入对象类型 
  221.      * @param c 对象Class 
  222.      * @param id 主键 
  223.      * @return T 返回该类型的对象 
  224.      * @throws Exception 
  225.      */  
  226.     public <T> T get(Class<T> c, Serializable id) throws Exception {  
  227.         T ety = null;  
  228.         try {  
  229.             ety = (T) this.getHibernateTemplate().get(c, id);             
  230.         } catch (Exception e) {  
  231.             throw new RuntimeException(e);  
  232.         }  
  233.         return ety;  
  234.     }  
  235.       
  236.     /** 
  237.      * <b>function:</b> 传入hql语句,查询对象 
  238.      * @createDate 2010-8-2 下午05:49:31 
  239.      * @author hoojo 
  240.      * @param <T> 返回对象类型 
  241.      * @param hql 查询的hql语句 
  242.      * @return 对象T 
  243.      * @throws Exception 
  244.      */  
  245.     public <T> T get(String hql) throws Exception {  
  246.         T ety = null;  
  247.         try {  
  248.             ety = (T) this.getSession().createQuery(hql).setMaxResults(1).uniqueResult();         
  249.         } catch (Exception e) {  
  250.             throw new RuntimeException(e);  
  251.         }  
  252.         return ety;  
  253.     }  
  254.       
  255.     /** 
  256.      * <b>function:</b> 通过hql语句查询List集合 
  257.      * @createDate 2010-8-2 下午05:51:05 
  258.      * @author hoojo 
  259.      * @param hql 查询hql语句 
  260.      * @return List<?> 
  261.      * @throws Exception 
  262.      */  
  263.     public <T> List<T> getList(String hql) throws Exception {  
  264.         List<T> list = null;  
  265.         try {  
  266.              list = (List<T>) this.getHibernateTemplate().find(hql);        
  267.         } catch (Exception e) {  
  268.             throw new RuntimeException(e);  
  269.         }  
  270.         return list;  
  271.     }  
  272.       
  273.     /** 
  274.      * <b>function:</b> 传入一个entity对象Class和Integer类型主键,返回该对象 
  275.      * @createDate 2010-8-2 下午05:47:20 
  276.      * @author hoojo 
  277.      * @param <T> 返回、传入对象类型 
  278.      * @param c 对象Class 
  279.      * @param id 主键 
  280.      * @return T 返回该类型的对象 
  281.      * @throws Exception 
  282.      */  
  283.     public <T> T getById(Class<T> c, Integer id) throws Exception {  
  284.         T ety = null;  
  285.         try {  
  286.             ety = (T) this.getHibernateTemplate().get(c, id);             
  287.         } catch (Exception e) {  
  288.             throw new RuntimeException(e);  
  289.         }  
  290.         return ety;  
  291.     }  
  292.       
  293.     /** 
  294.      * <b>function:</b> 传入一个entity对象Class和String型主键,返回该对象 
  295.      * @createDate 2010-8-2 下午05:44:53 
  296.      * @author hoojo 
  297.      * @param <T> 返回、传入对象类型 
  298.      * @param c 对象Class 
  299.      * @param id 主键 
  300.      * @return T 返回传入类型对象 
  301.      * @throws Exception 
  302.      */  
  303.     public <T> T getById(Class<T> c, String id) throws Exception {  
  304.         T ety = null;  
  305.         try {  
  306.             ety = (T) this.getHibernateTemplate().get(c, id);             
  307.         } catch (Exception e) {  
  308.             throw new RuntimeException(e);  
  309.         }  
  310.         return ety;  
  311.     }  
  312.       
  313.     /** 
  314.      * <b>function:</b> 传入hql查询语句和object数组类型的参数,返回查询list集合 
  315.      * @createDate 2010-8-2 下午05:52:36 
  316.      * @author hoojo 
  317.      * @param hql 查询的hql语句 
  318.      * @param obj 查询参数 
  319.      * @return 返回list集合 
  320.      * @throws Exception 
  321.      */  
  322.     public <T> List<T> getList(String hql, Object[] obj) throws Exception {  
  323.         List<T> list = null;  
  324.         try {  
  325.              list = (List<T>) this.getHibernateTemplate().find(hql, obj);       
  326.         } catch (Exception e) {  
  327.             throw new RuntimeException(e);  
  328.         }  
  329.         return list;  
  330.     }  
  331.       
  332.     /** 
  333.      * <b>function:</b> 传入一个将要删除的entity对象,返回删除是否成功 
  334.      * @createDate 2010-8-2 下午05:42:02 
  335.      * @author hoojo 
  336.      * @param <T> 传入对象类型 
  337.      * @param entity 将要传入的对象 
  338.      * @return boolean true/false 
  339.      * @throws Exception 
  340.      */  
  341.     public <T> boolean remove(T entity) throws Exception {  
  342.         boolean bo = false;  
  343.         try {  
  344.             this.getHibernateTemplate().delete(entity);   
  345.             bo = true;  
  346.         } catch (Exception e) {  
  347.             bo = false;  
  348.             throw new RuntimeException(e);  
  349.         }  
  350.         return bo;  
  351.     }  
  352.       
  353.     /** 
  354.      * <b>function:</b> 传入删除的hql语句,删除记录 
  355.      * @createDate 2010-8-3 上午09:53:49 
  356.      * @author hoojo 
  357.      * @param hql 将要被执行删除的hql语句 
  358.      * @return 是否删除成功 
  359.      * @throws Exception 
  360.      */  
  361.     public boolean remove(String hql) throws Exception {  
  362.         try {  
  363.             return this.executeByHql(hql) > 0 ? true : false;  
  364.         } catch (Exception e) {  
  365.             throw new RuntimeException(e);  
  366.         }  
  367.     }  
  368.       
  369.     /** 
  370.      * <b>function:</b> 动态查询 
  371.      * @createDate 2010-8-3 上午10:53:37 
  372.      * @author hoojo 
  373.      * @param <T> 查询类的类型 
  374.      * @param c 动态查询组合对象 
  375.      * @return list集合 
  376.      * @throws Exception 
  377.      */  
  378.     public <T> List<T> getList(Class<T> c) throws Exception {  
  379.         List<T> list = null;  
  380.         try {  
  381.             this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c));  
  382.         } catch (Exception e) {  
  383.             throw new RuntimeException(e);  
  384.         }  
  385.         return list;  
  386.     }  
  387.       
  388.     /** 
  389.      * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; 
  390.      * list集合保存总记录调试和记录结果 
  391.      * @createDate 2010-8-2 下午05:54:01 
  392.      * @author hoojo 
  393.      * @param queryHql 查询记录hql语句 
  394.      * @param queryCountHql 查询记录条数hql语句 
  395.      * @param firstResult 当前查询页 
  396.      * @param maxResult 每页显示多少条 
  397.      * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 
  398.      * @throws Exception 
  399.      */  
  400.     public List<?> showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception {  
  401.         List<Object> list = new ArrayList<Object>();  
  402.         try {  
  403.             Session session = this.getSession();  
  404.             list.add(session.createQuery(queryHql)  
  405.                     .setFirstResult(firstResult).setMaxResults(maxResult).list());   
  406.             list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult());  
  407.         } catch (Exception e) {  
  408.             throw new RuntimeException(e);  
  409.         }  
  410.         return list;  
  411.     }  
  412.       
  413.     /** 
  414.      * <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; 
  415.      * @createDate 2010-8-3 上午11:16:59 
  416.      * @author hoojo 
  417.      * @param queryHql list集合结果查询 
  418.      * @param queryCountHql 总记录调试查询 
  419.      * @param page 分页对象 
  420.      * @throws Exception 
  421.      */  
  422.     public <T> void showPage(String queryHql, String queryCountHql, Page<T> page) throws Exception {  
  423.         try {  
  424.             Session session = this.getSession();  
  425.             page.setResult(session.createQuery(queryHql)  
  426.                     .setFirstResult(page.getCurrentPage()).setMaxResults(page.getPageSize()).list());   
  427.             page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString()));  
  428.         } catch (Exception e) {  
  429.             throw new RuntimeException(e);  
  430.         }  
  431.     }  
  432.       
  433.     /** 
  434.      * <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 
  435.      * @createDate 2010-8-3 上午11:04:39 
  436.      * @author hoojo 
  437.      * @param queryCountHql hql查询count语句总条数 
  438.      * @param cResult  DetachedCriteria 动态查询条件 
  439.      * @param firstResult 起始 
  440.      * @param maxResult 最大页数 
  441.      * @return List<?> 查询集合 
  442.      * @throws Exception 
  443.      */  
  444.     public List<?> showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception {  
  445.         List<Object> list = new ArrayList<Object>();  
  446.         try {  
  447.             Session session = this.getSession();  
  448.             list.add(this.getHibernateTemplate().findByCriteria(cResult, firstResult, maxResult));   
  449.             list.add(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult());  
  450.         } catch (Exception e) {  
  451.             throw new RuntimeException(e);  
  452.         }  
  453.         return list;  
  454.     }  
  455.       
  456.     /** 
  457.      * <b>function:</b> 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity 
  458.      * @createDate 2010-8-3 上午11:14:30 
  459.      * @author hoojo 
  460.      * @param queryCountHql 查询count语句 
  461.      * @param cResult DetachedCriteria 动态查询组合类 
  462.      * @param page Page分页实体类 
  463.      * @throws Exception 
  464.      */  
  465.     public <T> void showPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception {  
  466.         try {  
  467.             Session session = this.getSession();  
  468.             page.setResult(this.getHibernateTemplate().findByCriteria(cResult, page.getCurrentPage(), page.getPageSize()));   
  469.             page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString()));  
  470.         } catch (Exception e) {  
  471.             throw new RuntimeException(e);  
  472.         }  
  473.     }  
  474.       
  475.     /** 
  476.      * <b>function:</b> 传入查询条件DetachedCriteria进行查询 
  477.      * @createDate 2010-8-3 上午11:55:28 
  478.      * @author hoojo 
  479.      * @param <T> 类型 
  480.      * @param dc DetachedCriteria动态条件查询 
  481.      * @return List 
  482.      * @throws Exception 
  483.      */  
  484.     public <T> List<T> find(DetachedCriteria dc) throws Exception {  
  485.         List<T> list = new ArrayList<T>();  
  486.         try {  
  487.             list = (List<T>) this.getHibernateTemplate().findByCriteria(dc);  
  488.         } catch (Exception e) {  
  489.             throw new RuntimeException(e);  
  490.         }  
  491.         return list;  
  492.     }  
  493.       
  494.     /** 
  495.      * <b>function:</b> 暴露基类session供用户使用 
  496.      * @createDate 2010-8-3 上午11:59:54 
  497.      * @author hoojo 
  498.      * @return Session 
  499.      */  
  500.     public Session session() {  
  501.         return this.getSession();  
  502.     }  
  503.       
  504.     /** 
  505.      * <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 
  506.      * @createDate 2010-8-3 上午11:58:51 
  507.      * @author hoojo 
  508.      * @return HibernateTemplate 
  509.      */  
  510.     public HibernateTemplate getTemplate() {  
  511.         return this.getHibernateTemplate();  
  512.     }  
  513. }  

 

分页对象Page.java:

[java] view plaincopyprint?
  1. package com.hoo.entity;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * <b>function:</b> 分页entity,封装分页数据 
  8.  * @project NetWorkService 
  9.  * @package com.hoo.entity  
  10.  * @fileName Page.java 
  11.  * @createDate 2010-8-3 上午10:32:03 
  12.  * @author hoojo 
  13.  * @email hoojo_@126.com 
  14.  * @blog http://blog.csdn.net/IBM_hoojo 
  15.  */  
  16. public class Page<T> {  
  17.     //当前页数  
  18.     private int currentPage;  
  19.     //总页数  
  20.     private int totalsPage;  
  21.     //每页显示记录条数  
  22.     private int pageSize;  
  23.     //总记录条数  
  24.     private int totalsCount;  
  25.     //查询返回结果  
  26.     private List<T> result = new ArrayList<T>();  
  27.     //分页链接  
  28.     private String uri;  
  29.       
  30.     public String getUri() {  
  31.         return uri;  
  32.     }  
  33.     public void setUri(String uri) {  
  34.         this.uri = uri;  
  35.     }  
  36.     public int getCurrentPage() {  
  37.         return currentPage;  
  38.     }  
  39.     public void setCurrentPage(int currentPage) throws Exception {  
  40.         if (currentPage < 0) {  
  41.             currentPage = 0;  
  42.         }  
  43.         this.currentPage = currentPage;  
  44.     }  
  45.     public int getTotalsPage() {  
  46.         try {  
  47.             if (totalsCount % pageSize == 0) {  
  48.                 totalsPage = totalsCount / pageSize;  
  49.             } else {  
  50.                 totalsPage = (totalsCount / pageSize) + 1;   
  51.             }  
  52.         } catch (Exception e) {  
  53.             throw new RuntimeException(e);  
  54.         }  
  55.         return totalsPage;  
  56.     }  
  57.     public void setTotalsPage(int totalsPage) {  
  58.         if (totalsPage < 0) {  
  59.             totalsPage = 0;  
  60.         }  
  61.         this.totalsPage = totalsPage;  
  62.     }  
  63.     public int getPageSize() {  
  64.         return pageSize;  
  65.     }  
  66.     public void setPageSize(int pageSize) {  
  67.         if (pageSize <= 0) {  
  68.             pageSize = 20;  
  69.         }  
  70.         this.pageSize = pageSize;  
  71.     }  
  72.     public int getTotalsCount() {  
  73.         return totalsCount;  
  74.     }  
  75.     public void setTotalsCount(int totalsCount) {  
  76.         if (totalsCount < 0) {  
  77.             totalsCount = 0;  
  78.         }  
  79.         this.totalsCount = totalsCount;  
  80.     }  
  81.     public List<T> getResult() {  
  82.         return result;  
  83.     }  
  84.     public void setResult(List<T> result) {  
  85.         this.result = result;  
  86.     }  
  87. }  

 

增删改查简单示例:

[java] view plaincopyprint?
  1. package com.hoo.dao.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.hoo.dao.IBaseDao;  
  6. import com.hoo.entity.UserInfo;  
  7.   
  8. public class TestDaoImpl<T extends UserInfo> implements ITestDao<T> {  
  9.     private IBaseDao dao;  
  10.     //setter方法注入dao  
  11.     public void setDao(IBaseDao dao) {  
  12.         this.dao = dao;  
  13.     }  
  14.       
  15.     //查询  
  16.     public <T extends UserInfo> List<T> getSortListByParentId(T entity) throws Exception {  
  17.         String hql = "....";  
  18.         if (entity == null || entity.getUserName() == null || "".equals(entity.getId())) {  
  19.             hql += " ...";  
  20.         } else {  
  21.             hql += " ...";  
  22.         }  
  23.         return dao.getList(hql);  
  24.     }  
  25.       
  26.     //添加  
  27.     public boolean addUser(T entity) {  
  28.         return dao.add(entity);  
  29.     }  
  30.       
  31.     //删除  
  32.     public boolean removeUser(T entity) {  
  33.         return dao.remove(entity);  
  34.     }  
  35.       
  36.     //修改  
  37.     public boolean editUser(T entity) {  
  38.         return dao.edit(entity);  
  39.     }  
  40. }  

 

为TestDaoImpl注入BaseDao即可

[xhtml] view plaincopyprint?
  1. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~dao 注入~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->  
  2. <!-- 数据库操作基类,所有的类要注入基类 -->  
  3. <bean id="baseDao" class="com.hhh.dao.impl.BaseDaoImpl">  
  4.     <property name="sessionFactory" ref="sessionFactory"/>  
  5. </bean>  
  6.   
  7. <!-- TestDao实现 -->  
  8. <bean id="testDao" class="com.hhh.dao.impl.TestDaoImpl">  
  9.      <!-- 注入基类BaseDaoImpl -->  
  10.     <property name="dao" ref="baseDao"/>  
  11. </bean>  

0 0
原创粉丝点击