BASEDAO 参考

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.Session;
import com.test.common.util.PaginationSupport;
@SuppressWarnings("unchecked")
public class BaseDao<T, ID extendsSerializable> extends HibernateDaoSupport
implements IBaseDao<T, ID>{
 private Log logger =LogFactory.getLog(getClass());

 protectedClass<T> entityClass;

 public BaseDao() {

 }

 protected Class getEntityClass() {
  if (entityClass == null){
   entityClass =(Class<T>) ((ParameterizedType)getClass()
     .getGenericSuperclass()).getActualTypeArguments()[0];
   logger.debug("Tclass = " + entityClass.getName());
  }
  return entityClass;
 }

 public void saveOrUpdate(T t) throwsDataAccessException {
  this.getHibernateTemplate().saveOrUpdate(t);
 }

 public T load(ID id) throwsDataAccessException {
  T load = (T)getHibernateTemplate().load(getEntityClass(), id);
  return load;
 }

 public T get(ID id) throws DataAccessException{
  T load = (T)getHibernateTemplate().get(getEntityClass(), id);
  return load;
 }

 public boolean contains(T t) throwsDataAccessException {
  returngetHibernateTemplate().contains(t);
 }

 public void delete(T t, LockMode lockMode)throws DataAccessException {
  getHibernateTemplate().delete(t,lockMode);
 }

 public void delete(T t) throwsDataAccessException {
  getHibernateTemplate().delete(t);
 }

 public voiddeleteAll(Collection<T> entities)throws DataAccessException {
  getHibernateTemplate().deleteAll(entities);
 }

 public List<T>find(String queryString, Object value)
   throwsDataAccessException {
  List<T>find = (List<T>)getHibernateTemplate().find(queryString, value);
  return find;
 }

 public List<T>find(String queryString, Object[] values)
   throwsDataAccessException {
  List<T>finds = (List<T>)getHibernateTemplate().find(queryString,values);
  return finds;
 }

 public List<T>find(String queryString) throws DataAccessException {
  return(List<T>)getHibernateTemplate().find(queryString);
 }

 public void refresh(T t, LockMode lockMode)throws DataAccessException {
  getHibernateTemplate().refresh(t,lockMode);
 }

 public void refresh(T t) throwsDataAccessException {
  getHibernateTemplate().refresh(t);
 }

 public Serializable save(T t) throwsDataAccessException {
  returngetHibernateTemplate().save(t);
 }

 public voidsaveOrUpdateAll(Collection<T>entities)
   throwsDataAccessException {
  getHibernateTemplate().saveOrUpdateAll(entities);
 }

 public void update(T t, LockMode lockMode)throws DataAccessException {
  getHibernateTemplate().update(t,lockMode);
 }

 public void update(T t) throwsDataAccessException {
  getHibernateTemplate().update(t);
 }


 public List<T>list() throws DataAccessException {
  returngetHibernateTemplate().loadAll(getEntityClass());

 }

 public List<T>findByNamedQuery(String queryName)
   throwsDataAccessException {
  returngetHibernateTemplate().findByNamedQuery(queryName);
 }

 public List<T>findByNamedQuery(String queryName, Object value)
   throwsDataAccessException {
  returngetHibernateTemplate().findByNamedQuery(queryName, value);
 }

 public List<T>findByNamedQuery(String queryName, Object[] values)
   throwsDataAccessException {
  returngetHibernateTemplate().findByNamedQuery(queryName, values);
 }

 public PaginationSupportfindPageByCriteria(
   finalDetachedCriteria detachedCriteria, final int pageSize,
   final intstartIndex) {
  return (PaginationSupport)getHibernateTemplate().execute(
    newHibernateCallback() {
     publicObject doInHibernate(org.hibernate.Session session)
       throwsHibernateException {
      Criteriacriteria = detachedCriteria
        .getExecutableCriteria(session);
      inttotalCount = ((Integer) criteria.setProjection(
        Projections.rowCount()).uniqueResult())
        .intValue();
      criteria.setProjection(null);
      Listitems = criteria.setFirstResult(startIndex)
        .setMaxResults(pageSize).list();
      PaginationSupportps = new PaginationSupport(items,
        totalCount,pageSize, startIndex);
      returnps;
     }
    },true);
 }

 public  PaginationSupportfindPageByQuery( final  String hql, final StringcountHql,final int pageSize,final int startIndex){
     return (PaginationSupport)getHibernateTemplate().execute(
     new  HibernateCallback() {
       public  Object doInHibernate(Sessionsession)  throws HibernateException, SQLException {
        inttotalCount=((Integer)session.createQuery(countHql).iterate().next()).intValue();
        Queryquery session.createQuery(hql);
             query.setFirstResult(startIndex);
             query.setMaxResults(pageSize);
             List items  = query.list();
          PaginationSupport ps = new PaginationSupport(items,
       totalCount, pageSize, startIndex);
          return ps;
             
             }
       },true);
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值