对HibernateDaoSupport进行二次封装:hibernate增删改查组件

  大发现,大家在贴代码的时候。系统会自动加上一些代码或注释之类的东西,像script或style会被加上其他字符。并且有2份相同的代码,这时你可以把在线编辑器中的textarea中的代码删掉。重新贴上去就可以了,注意是重新贴上上去,不是重新插入!!这样就没有问题了,呵呵~~~
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  本组件继承了HibernateDaoSupport,并完成对HibernateDaoSupport进行二次封装。提取平时开发常用的底层操作方法,并根据个
  人习惯,定义自己的编码规范。根据sun官方的最新开发规范,使用了jdk的新特性--泛型。所有的操作对象以泛型指定。
  定义接口规范:IBaseDao.java package com.hoo.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.HibernateTempla te; import com.hoo.entity.Page; /*** * function: 增删改查组件规范接口 * @project NetWorkService * @package com.hoo.dao * @fileName IBaseDao.java * @createDate 2010-8-2 下午05:28:03 * @author hoojo * @email hoojo_@126.com * @blog http://blog.csdn.net/IBM_hoojo */ public interface IBaseDao { /** * function: 增加一个entity对象,返回是否添加成功 * @createDate 2010-8-2 下午05:28:38 * @author hoojo * @param 对象类型 * @param entity 对象 * @return boolean true/false * @throws Exception */ public boolean add(T entity) throws Exception; /** * function: 添加一个entity对象,返回添加对象的Integer类型的主键 * @createDate 2010-8-2 下午05:29:39 * @author hoojo * @param 对象类型 * @param entity 将要添加的对象 * @return Integer 返回主键 * @throws Exception */ public Integer addAndGetId4Integer(T entity) throws Exception; /** * function: 添加一个对象并且返回该对象的String类型的主键 * @createDate 2010-8-2 下午05:31:32 * @author hoojo * @param 对象类型 * @param entity 将要添加的对象 * @return String 返回的主键 * @throws Exception */ public String addAndGetId4String(T entity) throws Exception; /** * function: 传入hql语句执行 * @createDate 2010-8-2 下午04:42:26 * @author hoojo * @param hql String hql语句 * @return int 影响行数 * @throws Exception */ public int executeByHql(String hql) throws Exception; /** * function: 传入hql语句执行查询,返回list集合 * @createDate 2010-8-3 上午10:00:34 * @author hoojo * @param hql 查询的hql语句 * @return List集合 * @throws Exception */ public List findByHql(String hql) throws Exception; /** * function: 执行原生态的sql语句,添加、删除、修改语句 * @createDate 2010-8-2 下午05:33:42 * @author hoojo * @param sql 将要执行的sql语句 * @return int * @throws Exception */ public int executeBySql(String sql) throws Exception; /** * function: 传入sql语句执行查询,返回list集合 * @createDate 2010-8-3 上午10:00:34 * @author hoojo * @param sql 查询的sql语句 * @return List集合 * @throws Exception */ public List findBySql(String sql) throws Exception; /** * function: 修改entity对象,返回是否修改成功 * @createDate 2010-8-2 下午05:35:47 * @author hoojo * @param 对象类型 * @param entity 将要修改的对象 * @return boolean true/false 是否修改成功 * @throws Exception */ public boolean edit(T entity) throws Exception; /** * function: 传入hql语句执行修改,返回是否修改成功 * @createDate 2010-8-2 下午05:36:31 * @author hoojo * @param hql 查询的hql语句 * @return boolean true/false 返回是否修改成功 * @throws Exception */ public boolean edit(String hql) throws Exception; /** * function: 执行修改的hql语句,返回修改的行数 * @createDate 2010-8-2 下午05:38:58 * @author hoojo * @param hql 修改语句 * @return int 返回修改的行数 * @throws Exception */ public int editByHql(String hql) throws Exception; /** * function: 传入一个将要删除的entity对象,返回删除是否成功 * @createDate 2010-8-2 下午05:42:02 * @author hoojo * @param 传入对象类型 * @param entity 将要传入的对象 * @return boolean true/false * @throws Exception */ public boolean remove(T entity) throws Exception; /** * function: 传入一个entity对象Class和String型主键,返回该对象 * @createDate 2010-8-2 下午05:44:53 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回传入类型对象 * @throws Exception */ public T getById(Class c, String id) throws Exception; /** * function: 传入一个entity对象Class和Integer类型主键,返回该对象 * @createDate 2010-8-2 下午05:47:20 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回该类型的对象 * @throws Exception */ public T getById(Class c, Integer id) throws Exception; /** * function: 传入一个entity对象Class和Serializable类型主键,返回该对象 * @createDate 2010-8-2 下午05:48:36 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回该类型的对象 * @throws Exception */ public T get(Class c, Serializable id) throws Exception; /** * function: 传入hql语句,查询对象 * @createDate 2010-8-2 下午05:49:31 * @author hoojo * @param 返回对象类型 * @param hql 查询的hql语句 * @return 对象T * @throws Exception */ public T get(String hql) throws Exception; /** * function: 通过hql语句查询List集合 * @createDate 2010-8-2 下午05:51:05 * @author hoojo * @param hql 查询hql语句 * @return List * @throws Exception */ public List getList(String hql) throws Exception; /** * function: 传入删除的hql语句,删除记录 * @createDate 2010-8-3 上午09:53:49 * @author hoojo * @param hql 将要被执行删除的hql语句 * @return 是否删除成功 * @throws Exception */ public boolean remove(String hql) throws Exception; /** * function: 动态查询 * @createDate 2010-8-3 上午10:53:37 * @author hoojo * @param 查询类的类型 * @param c 动态查询组合对象 * @return list集合 * @throws Exception */ public List getList(Class c) throws Exception; /** * function: 传入hql查询语句和object数组类型的参数,返回查询list集合 * @createDate 2010-8-2 下午05:52:36 * @author hoojo * @param hql 查询的hql语句 * @param obj 查询参数 * @return 返回list集合 * @throws Exception */ public List getList(String hql, Object[] obj) throws Exception; /** * function: 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; * list集合保存总记录调试和记录结果 * @createDate 2010-8-2 下午05:54:01 * @author hoojo * @param queryHql 查询记录hql语句 * @param queryCountHql 查询记录条数hql语句 * @param firstResult 当前查询页 * @param maxResult 每页显示多少条 * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 * @throws Exception */ public List showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception; /** * function: 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; * @createDate 2010-8-3 上午11:16:59 * @author hoojo * @param queryHql list集合结果查询 * @param queryCountHql 总记录调试查询 * @param page 分页对象 * @throws Exception */ public void showPage(String queryHql, String queryCountHql, Page page) throws Exception; /** * function: 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 * @createDate 2010-8-3 上午11:04:39 * @author hoojo * @param queryCountHql hql查询count语句总条数 * @param cResult DetachedCriteria 动态查询条件 * @param firstResult 起始 * @param maxResult 最大页数 * @return List 查询集合 * @throws Exception */ public List showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception; /** * function: 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity * @createDate 2010-8-3 上午11:14:30 * @author hoojo * @param queryCountHql 查询count语句 * @param cResult DetachedCriteria 动态查询组合类 * @param page Page分页实体类 * @throws Exception */ public void showPage(String queryCountHql, DetachedCriteria cResult, Page page) throws Exception; /** * function: 传入查询条件DetachedCriteria进行查询 * @createDate 2010-8-3 上午11:55:28 * @author hoojo * @param 类型 * @param dc DetachedCriteria动态条件查询 * @return List * @throws Exception */ public List find(DetachedCriteria dc) throws Exception; /** * function: 暴露基类session供用户使用 * @createDate 2010-8-3 上午11:59:54 * @author hoojo * @return Session */ public Session session(); /** * function: 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 * @createDate 2010-8-3 上午11:58:51 * @author hoojo * @return HibernateTemplate */ public HibernateTemplate getTemplate(); } 下面是实现IBaseDao接口的代码BaseDaoImpl.java: package com.hoo.dao.impl; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.HibernateTempla te; import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport; import com.hoo.dao.IBaseDao; import com.hoo.entity.Page; /** * function: 删除改查组件实现类 * @project NetWorkService * @package com.hhh.dao * @fileName BaseDAOImpl.java * @createDate 2010-8-2 下午05:58:45 * @author hoojo * @email hoojo_@126.com * @blog http://blog.csdn.net/IBM_hoojo */ @SuppressWarnings("unchecked") public class BaseDaoImpl extends HibernateDaoSupport implements IBaseDao { /** * function: 增加一个entity对象,返回是否添加成功 * @createDate 2010-8-2 下午05:28:38 * @author hoojo * @param 对象类型 * @param entity 对象 * @return boolean true/false * @throws Exception */ public boolean add(T entity) throws Exception { boolean bo = false; try { Serializable io = this.getHibernateTemplate().save(entity); if (io != null) { bo = true; } } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } /** * function: 添加一个entity对象,返回添加对象的Integer类型的主键 * @createDate 2010-8-2 下午05:29:39 * @author hoojo * @param 对象类型 * @param entity 将要添加的对象 * @return Integer 返回主键 * @throws Exception */ public Integer addAndGetId4Integer(T entity) throws Exception { Integer id = null; try { id = (Integer) this.getHibernateTemplate().save(entity); } catch (Exception e) { throw new RuntimeException(e); } return id; } /** * function: 添加一个对象并且返回该对象的String类型的主键 * @createDate 2010-8-2 下午05:31:32 * @author hoojo * @param 对象类型 * @param entity 将要添加的对象 * @return String 返回的主键 * @throws Exception */ public String addAndGetId4String(T entity) throws Exception { String id = null; try { id = (String) this.getHibernateTemplate().save(entity); } catch (Exception e) { throw new RuntimeException(e); } return id; } /** * function: 修改entity对象,返回是否修改成功 * @createDate 2010-8-2 下午05:35:47 * @author hoojo * @param 对象类型 * @param entity 将要修改的对象 * @return boolean true/false 是否修改成功 * @throws Exception */ public boolean edit(T entity) throws Exception { boolean bo = false; try { this.getHibernateTemplate().update(entity); bo = true; } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } /** * function: 传入hql语句执行修改,返回是否修改成功 * @createDate 2010-8-2 下午05:36:31 * @author hoojo * @param hql 查询的hql语句 * @return boolean true/false 返回是否修改成功 * @throws Exception */ public boolean edit(String hql) throws Exception { boolean bo = false; try { int count = this.getHibernateTemplate().bulkUpdate(hql); bo = count > 0 ? true : false; } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } /** * function: 执行修改的hql语句,返回修改的行数 * @createDate 2010-8-2 下午05:38:58 * @author hoojo * @param hql 修改语句 * @return int 返回修改的行数 * @throws Exception */ public int editByHql(String hql) throws Exception { int count = 0; try { count = this.getHibernateTemplate().bulkUpdate(hql); } catch (Exception e) { throw new RuntimeException(e); } return count; } /** * function: 传入hql语句执行 * @createDate 2010-8-2 下午04:42:26 * @author hoojo * @param hql String hql语句 * @return int 影响行数 * @throws Exception */ public int executeByHql(String hql) throws Exception { try { return this.getHibernateTemplate().bulkUpdate(hql); } catch (Exception e) { throw new RuntimeException(e); } } /** * function: 传入hql语句执行查询,返回list集合 * @createDate 2010-8-3 上午10:00:34 * @author hoojo * @param hql 查询的hql语句 * @return List集合 * @throws Exception */ public List findByHql(String hql) throws Exception { List list = null; try { list = (List) this.getHibernateTemplate().find(hql); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 执行原生态的sql语句,添加、删除、修改语句 * @createDate 2010-8-2 下午05:33:42 * @author hoojo * @param sql 将要执行的sql语句 * @return int * @throws Exception */ public int executeBySql(String sql) throws Exception { try { return this.getSession().createSQLQuery(sql).executeUpdat e(); } catch (Exception e) { throw new RuntimeException(e); } } /** * function: 传入sql语句执行查询,返回list集合 * @createDate 2010-8-3 上午10:00:34 * @author hoojo * @param sql 查询的sql语句 * @return List集合 * @throws Exception */ public List findBySql(String sql) throws Exception { List list = null; try { list = (List) this.getSession().createSQLQuery(sql).list(); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 传入一个entity对象Class和Serializable类型主键,返回该对象 * @createDate 2010-8-2 下午05:48:36 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回该类型的对象 * @throws Exception */ public T get(Class c, Serializable id) throws Exception { T ety = null; try { ety = (T) this.getHibernateTemplate().get(c, id); } catch (Exception e) { throw new RuntimeException(e); } return ety; } /** * function: 传入hql语句,查询对象 * @createDate 2010-8-2 下午05:49:31 * @author hoojo * @param 返回对象类型 * @param hql 查询的hql语句 * @return 对象T * @throws Exception */ public T get(String hql) throws Exception { T ety = null; try { ety = (T) this.getSession().createQuery(hql).setMaxResults(1 ).uniqueResult(); } catch (Exception e) { throw new RuntimeException(e); } return ety; } /** * function: 通过hql语句查询List集合 * @createDate 2010-8-2 下午05:51:05 * @author hoojo * @param hql 查询hql语句 * @return List * @throws Exception */ public List getList(String hql) throws Exception { List list = null; try { list = (List) this.getHibernateTemplate().find(hql); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 传入一个entity对象Class和Integer类型主键,返回该对象 * @createDate 2010-8-2 下午05:47:20 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回该类型的对象 * @throws Exception */ public T getById(Class c, Integer id) throws Exception { T ety = null; try { ety = (T) this.getHibernateTemplate().get(c, id); } catch (Exception e) { throw new RuntimeException(e); } return ety; } /** * function: 传入一个entity对象Class和String型主键,返回该对象 * @createDate 2010-8-2 下午05:44:53 * @author hoojo * @param 返回、传入对象类型 * @param c 对象Class * @param id 主键 * @return T 返回传入类型对象 * @throws Exception */ public T getById(Class c, String id) throws Exception { T ety = null; try { ety = (T) this.getHibernateTemplate().get(c, id); } catch (Exception e) { throw new RuntimeException(e); } return ety; } /** * function: 传入hql查询语句和object数组类型的参数,返回查询list集合 * @createDate 2010-8-2 下午05:52:36 * @author hoojo * @param hql 查询的hql语句 * @param obj 查询参数 * @return 返回list集合 * @throws Exception */ public List getList(String hql, Object[] obj) throws Exception { List list = null; try { list = (List) this.getHibernateTemplate().find(hql, obj); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 传入一个将要删除的entity对象,返回删除是否成功 * @createDate 2010-8-2 下午05:42:02 * @author hoojo * @param 传入对象类型 * @param entity 将要传入的对象 * @return boolean true/false * @throws Exception */ public boolean remove(T entity) throws Exception { boolean bo = false; try { this.getHibernateTemplate().delete(entity); bo = true; } catch (Exception e) { bo = false; throw new RuntimeException(e); } return bo; } /** * function: 传入删除的hql语句,删除记录 * @createDate 2010-8-3 上午09:53:49 * @author hoojo * @param hql 将要被执行删除的hql语句 * @return 是否删除成功 * @throws Exception */ public boolean remove(String hql) throws Exception { try { return this.executeByHql(hql) > 0 ? true : false; } catch (Exception e) { throw new RuntimeException(e); } } /** * function: 动态查询 * @createDate 2010-8-3 上午10:53:37 * @author hoojo * @param 查询类的类型 * @param c 动态查询组合对象 * @return list集合 * @throws Exception */ public List getList(Class c) throws Exception { List list = null; try { this.getHibernateTemplate().findByCriteria(Detache dCriteria.forClass(c)); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; * list集合保存总记录调试和记录结果 * @createDate 2010-8-2 下午05:54:01 * @author hoojo * @param queryHql 查询记录hql语句 * @param queryCountHql 查询记录条数hql语句 * @param firstResult 当前查询页 * @param maxResult 每页显示多少条 * @return List返回集合 集合0保存查询结果、集合1保存总记录条数 * @throws Exception */ public List showPage(String queryHql, String queryCountHql, int firstResult, int maxResult) throws Exception { List list = new ArrayList(); try { Session session = this.getSession(); list.add(session.createQuery(queryHql) .setFirstResult(firstResult).setMaxResults(maxResu lt).list()); list.add(session.createQuery(queryCountHql).setMax Results(1).uniqueResult()); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合; * @createDate 2010-8-3 上午11:16:59 * @author hoojo * @param queryHql list集合结果查询 * @param queryCountHql 总记录调试查询 * @param page 分页对象 * @throws Exception */ public void showPage(String queryHql, String queryCountHql, Page page) throws Exception { try { Session session = this.getSession(); page.setResult(session.createQuery(queryHql) .setFirstResult(page.getCurrentPage()).setMaxResul ts(page.getPageSize()).list()); page.setTotalsCount(Integer.parseInt(session.creat eQuery(queryCountHql).setMaxResults(1).uniqueResult ().toString())); } catch (Exception e) { throw new RuntimeException(e); } } /** * function: 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页 * @createDate 2010-8-3 上午11:04:39 * @author hoojo * @param queryCountHql hql查询count语句总条数 * @param cResult DetachedCriteria 动态查询条件 * @param firstResult 起始 * @param maxResult 最大页数 * @return List 查询集合 * @throws Exception */ public List showPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception { List list = new ArrayList(); try { Session session = this.getSession(); list.add(this.getHibernateTemplate().findByCriteri a(cResult, firstResult, maxResult)); list.add(session.createQuery(queryCountHql).setMax Results(1).uniqueResult()); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity * @createDate 2010-8-3 上午11:14:30 * @author hoojo * @param queryCountHql 查询count语句 * @param cResult DetachedCriteria 动态查询组合类 * @param page Page分页实体类 * @throws Exception */ public void showPage(String queryCountHql, DetachedCriteria cResult, Page page) throws Exception { try { Session session = this.getSession(); page.setResult(this.getHibernateTemplate().findByC riteria(cResult, page.getCurrentPage(), page.getPageSize())); page.setTotalsCount(Integer.parseInt(session.creat eQuery(queryCountHql).setMaxResults(1).uniqueResult ().toString())); } catch (Exception e) { throw new RuntimeException(e); } } /** * function: 传入查询条件DetachedCriteria进行查询 * @createDate 2010-8-3 上午11:55:28 * @author hoojo * @param 类型 * @param dc DetachedCriteria动态条件查询 * @return List * @throws Exception */ public List find(DetachedCriteria dc) throws Exception { List list = new ArrayList(); try { list = (List) this.getHibernateTemplate().findByCriteria(dc); } catch (Exception e) { throw new RuntimeException(e); } return list; } /** * function: 暴露基类session供用户使用 * @createDate 2010-8-3 上午11:59:54 * @author hoojo * @return Session */ public Session session() { return this.getSession(); } /** * function: 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作 * @createDate 2010-8-3 上午11:58:51 * @author hoojo * @return HibernateTemplate */ public HibernateTemplate getTemplate() { return this.getHibernateTemplate(); } } 分页对象Page.java: package com.hoo.entity; import java.util.ArrayList; import java.util.List; /** * function: 分页entity,封装分页数据 * @project NetWorkService * @package com.hoo.entity * @fileName Page.java * @createDate 2010-8-3 上午10:32:03 * @author hoojo * @email hoojo_@126.com * @blog http://blog.csdn.net/IBM_hoojo */ public class Page { //当前页数 private int currentPage; //总页数 private int totalsPage; //每页显示记录条数 private int pageSize; //总记录条数 private int totalsCount; //查询返回结果 private List result = new ArrayList(); //分页链接 private String uri; public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) throws Exception { if (currentPage getResult() { return result; } public void setResult(List result) { this.result = result; } } 增删改查简单示例: package com.hoo.dao.impl; import java.util.List; import com.hoo.dao.IBaseDao; import com.hoo.entity.UserInfo; public class TestDaoImpl implements ITestDao { private IBaseDao dao; //setter方法注入dao public void setDao(IBaseDao dao) { this.dao = dao; } //查询 public List getSortListByParentId(T entity) throws Exception { String hql = "...."; if (entity == null || entity.getUserName() == null || "".equals(entity.getId())) { hql += " ..."; } else { hql += " ..."; } return dao.getList(hql); } //添加 public boolean addUser(T entity) { return dao.add(entity); } //删除 public boolean removeUser(T entity) { return dao.remove(entity); } //修改 public boolean editUser(T entity) { return dao.edit(entity); } } 为TestDaoImpl注入BaseDao即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值