| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
dd2d
10年前发布

通用的Java DAO类

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;      }    }