通用的Java DAO类
发布时间:2020-12-14 23:37:55 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.Serializable;import java.util.Date;import java.util.List;import org.hibernate.Criteria;import org.hibernate.HibernateExceptio
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import java.io.Serializable; import java.util.Date; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Restrictions; import org.jb.common.dao.CommonDAO; import org.jb.common.util.PageResult; import org.jb.y272.team0.entity.SalaryStandard; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class CommonDAOHibImpl extends HibernateDaoSupport implements CommonDAO { /* * (非 Javadoc) * * @see y2ssh.dlc.chp2.dao.CommonDAO#add(java.lang.Object) */ public Serializable add(Object o) { Serializable ret = super.getHibernateTemplate().save(o); return ret; } /* * (非 Javadoc) * * @see y2ssh.dlc.chp2.dao.CommonDAO#get(java.lang.Class,* java.io.Serializable) */ public Object get(Class clazz,Serializable id) { Object ret = super.getHibernateTemplate().get(clazz,id); return ret; } /* * (非 Javadoc) * * @see y2ssh.dlc.chp2.dao.CommonDAO#del(java.lang.Class,* java.io.Serializable) */ public void del(Class clazz,Serializable id) { super.getHibernateTemplate().delete(this.get(clazz,id)); } /* * (非 Javadoc) * * @see y2ssh.dlc.chp2.dao.CommonDAO#update(java.lang.Object) */ public void update(Object o) { super.getHibernateTemplate().update(o); } /* * (非 Javadoc) * * @see y2ssh.dlc.chp2.dao.CommonDAO#search(java.lang.String) */ public List list(String hql) { List ret = super.getHibernateTemplate().find(hql); return ret; } public List listOrderBy(String hql) { List ret = super.getHibernateTemplate().find(hql); return ret; } /* * (非 Javadoc) * * @see org.jb.common.dao.CommonDAO#listByPage(java.lang.String,* org.jb.common.util.PageResult) */ @SuppressWarnings("unchecked") public void listByPage(String hql,PageResult pageResult) { if (null == hql) { return; } Query query = this.getSession().createQuery(hql); query.setFirstResult(pageResult.getFirstRec()); query.setMaxResults(pageResult.getPageSize()); List ret = query.list(); pageResult.setList(ret); String queryString = ""; if (hql.toUpperCase().indexOf("SELECT") != -1) { int i = query.getQueryString().toUpperCase().indexOf("FROM"); queryString = "Select count(*) " + hql.substring(i,hql.length()); } else { queryString = "Select count(*) " + hql; } // 去掉ORDER BY 的部分 int j = queryString.toUpperCase().lastIndexOf("ORDER"); if (j != -1) { queryString = queryString.substring(0,j); } Query cquery = this.getSession().createQuery(queryString); cquery.setCacheable(true); if(cquery.iterate().hasNext()){ int recTotal = ((Integer) cquery.iterate().next()).intValue(); pageResult.setRecTotal(recTotal); } } @SuppressWarnings("unchecked") public List<SalaryStandard> getStandards(final Date beginTime,final Date endTime,final SalaryStandard standard) { List<SalaryStandard> list = super.getHibernateTemplate().executeFind( new HibernateCallback() { public Object doInHibernate(Session s) throws HibernateException { Criteria c = s.createCriteria(SalaryStandard.class); if (standard.getStandardId() != null && standard.getStandardId()!="") { c.add(Restrictions.like("standardId",standard .getStandardId(),MatchMode.ANYWHERE)); } if (standard.getDesigner() != null && standard.getDesigner()!="") { c.add(Restrictions.like("designer",standard .getDesigner(),MatchMode.ANYWHERE)); } if (beginTime != null) { c.add(Restrictions.ge("registTime",beginTime)); } if (endTime != null) { c.add(Restrictions.le("registTime",endTime)); } return c.list(); } }); return list; } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读