BlogJava 联系 聚合 管理  

Blog Stats

随笔档案(17)

文章档案(1)


GaoWei

package ynzc.example.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ynzc.example.dao.Dao;

/**
 * TODO:xxxxx
 * <br>
 * <b>Company</b>:
 *             Yunnan Zhong Cheng Technological Development Co., Ltd.
 * <br>
 * <b>Writer</b>:
 *            <a href="mailto:gggg874@126.com">gaowei,qq:8597034</a><br>
 * <b>CreateTime<b>:
 *            May 12, 2007 12:34:10 PM
 */
public class DaoImpl extends HibernateDaoSupport implements Dao{

  private void doFilter(DetachedCriteria criteria,Map filter){
    if(MapUtils.isNotEmpty(filter)){
      for(Object fieldName:filter.keySet()){
        Object value = filter.get(fieldName);
        if (((String)fieldName).indexOf('.') > 0) {
            String[] sArray = StringUtils.split((String)fieldName, '.');
            for (int i = 0; i < sArray.length; i++) {
                if (i == 0)
                    criteria.createAlias(sArray[i], "alias_"
                            + sArray[i]);
                else if (i > 0 && i < sArray.length - 1)
                    criteria.createAlias("alias_" + sArray[i - 1] + "."
                            + sArray[i], "alias_" + sArray[i]);
                else
                    fieldName = "alias_" + sArray[i - 1] + "."
                            + sArray[i];
            }
        }
        criteria.add(value==null?Restrictions.isNull((String) fieldName):Restrictions.eq((String)fieldName,value));
      }
    }
  }
 
  private void doSort(DetachedCriteria criteria,Map sort){
    if (MapUtils.isNotEmpty(sort)) {
        for (Object fieldName : sort.keySet()) {
            String orderType = (String) sort.get(fieldName);
            criteria.addOrder(StringUtils
                    .equalsIgnoreCase("asc", orderType) ? Order
                    .asc((String) fieldName) : Order
                    .desc((String) fieldName));
        }
    }
  }
 
  public List findAllBy(Class clazz, String name, Object value) {
    DetachedCriteria criteria=DetachedCriteria.forClass(clazz);
    criteria.add(Restrictions.eq(name,value));
    return findByCriteria(criteria);
  }

  public List findAllBy(Class clazz, Map filter, Map sorter,int...param) {
    DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
    doFilter(criteria, filter);
    doSort(criteria, sorter);
    return this.findByCriteria(criteria, param);
  }

  public Object findBy(Class clazz, String name, Object value) {
    DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
    criteria.add(Restrictions.eq(name, value));
    List list = this.findByCriteria(criteria, 1, 1);
    return CollectionUtils.isEmpty(list) ? null : list.iterator().next();
  }

  public List findByCriteria(DetachedCriteria criteria, int... param) {
    if(param.length>0){
      int max=param.length>1?param[1]:15;
      int first=param[0]>1?(param[0]-1)*max:0;
      return getHibernateTemplate().findByCriteria(criteria, first, max);
    }
   return getHibernateTemplate().findByCriteria(criteria);
  }

  public List findByHql(String hql, Object... param) {
    if (param.length == 0)
        return getHibernateTemplate().find(hql);
    else
        return getHibernateTemplate().find(hql, param);
  }

  public List findByHql(final String hql,final int pageNo,final int pageSize,final Object... param) {
    return this.getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session)
                throws HibernateException, SQLException {
            Query query = session.createQuery(hql);
            if (param != null) {
                for (int i = 0; i < param.length; i++) {
                    query.setParameter(i, param[i]);
                }
            }
            if (pageNo > 0) {
                int maxResults = pageSize > 0 ? pageSize : 15;
                int firstResult = pageNo > 1 ? (pageNo - 1) * maxResults : 0;
                query.setFirstResult(firstResult);
                query.setMaxResults(maxResults);
            }
            return query.list();
        }
    });
  }

  public Object getObject(Class clazz, Serializable id) {
    Object o = getHibernateTemplate().get(clazz, id);
    if (o == null) {
        throw new ObjectRetrievalFailureException(clazz, id);
    }
    return o;
  }

  public List getObjects(Class clazz) {
   return getHibernateTemplate().loadAll(clazz);
  }

  public void removeObject(Class clazz, Serializable id) {
   
    getHibernateTemplate().delete(getObject(clazz,id));
  }

  public void removeObject(Collection collection) {
    getHibernateTemplate().deleteAll(collection);
  }

  public void removeObject(Object object) {
    getHibernateTemplate().delete(object);
  }

  public Serializable savaObject(Object object) {
    return getHibernateTemplate().save(object);
  }

  public int totalFilter(Class clazz, Map filter) {
    DetachedCriteria criteria=DetachedCriteria.forClass(clazz);
    doFilter(criteria,filter);
    criteria.setProjection(Projections.rowCount());
    return ((Number)this.findByCriteria(criteria).iterator().next()).intValue();
  }

  public void updateObject(Object object) {
    getHibernateTemplate().update(object);
  }

}


posted on 2007-05-12 12:34 gggg874 阅读(701) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: