通用的Java DAO类

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
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;
    }

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇: java自动识别用户上传的文本文件编码

下一篇:JS实现无刷新倒计时