BlogJava 联系 聚合 管理  

Blog Stats

随笔档案(17)

文章档案(1)


GaoWei

          
                 


package cn.ynzc.certificateQuery.hibernate;

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

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

import cn.ynzc.certificateQuery.dao.Dao;

public class DaoImpl extends HibernateDaoSupport implements Dao {
 public List findAllBy(final Class clazz, final String name,
   final Object value) {
  return (List) getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Criteria criteria = session.createCriteria(clazz);
    criteria.add(Restrictions.eq(name, value));
    return criteria.list();
   }
  });
 }

 public List findAllBy(final Class clazz, final Map filter, final Map sort,
   final int pageNo, final int pageSize) {
  return (List) getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Criteria criteria = session.createCriteria(clazz);
    criteriaFilter(criteria, filter);
    criteriaSort(criteria, sort);
    if (pageNo != -1) {
     criteria.setFirstResult(pageNo - 1 > 0 ? (pageNo - 1)
       * pageSize : 0);
     criteria.setMaxResults(pageSize);
    }
    return criteria.list();
   }
  });
 }

 private void criteriaFilter(Criteria criteria, Map filter) {
  if (MapUtils.isNotEmpty(filter)) {
   for (Iterator iterator = filter.keySet().iterator(); iterator
     .hasNext();) {
    String fieldName = (String) iterator.next();
    Object value = filter.get(fieldName);
    if (fieldName.indexOf('.') > 0) {
     String[] sArray = StringUtils.split(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(fieldName)
      : Restrictions.eq(fieldName, value));
   }
  }
 }

 private void criteriaSort(Criteria criteria, Map sort) {
  if (MapUtils.isNotEmpty(sort)) {
   for (Iterator iterator = sort.keySet().iterator(); iterator
     .hasNext();) {
    String key = (String) iterator.next();
    criteria.addOrder(StringUtils.equalsIgnoreCase("asc",
      (String) sort.get(key)) ? Order.asc(key) : Order
      .desc(key));
   }
  }
 }

 public Object findBy(final Class clazz, final String name,
   final Object value) {
  return getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Criteria criteria = session.createCriteria(clazz);
    criteria.add(Restrictions.eq(name, value));
    List list = criteria.list();
    return list == null || list.isEmpty() ? null : list.get(0);
   }
  });
 }

 public Object getObject(Class clazz, Serializable id) {
  return getHibernateTemplate().get(clazz, id);
 }

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

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

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

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

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

 public int total(final Class clazz, final Map filter) {
  return ((Number) getHibernateTemplate().execute(
    new HibernateCallback() {
     public Object doInHibernate(Session session)
       throws HibernateException, SQLException {
      Criteria criteria = session.createCriteria(clazz);
      criteriaFilter(criteria, filter);
      criteria.setProjection(Projections.rowCount());
      criteria.setCacheable(true);
      return criteria.uniqueResult();
     }
    })).intValue();
 }

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

}

posted on 2007-05-02 04:55 gggg874 阅读(384) 评论(0)  编辑  收藏

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


网站导航: