请大家讨论一下hibernate这样的写法的优点和不足

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

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;



/**
* @描述: 公共DAO层实现
*/
@SuppressWarnings("unchecked")
public class CommonDaoImpl extends HibernateDaoSupport implements ICommonDao {

public Serializable add(Object object) throws Exception{
  return (Serializable) super.getHibernateTemplate().save(object);
}

public void del(Object object)  throws Exception{
  super.getHibernateTemplate().delete(object);
}

public void update(Object object) throws Exception{
  super.getHibernateTemplate().update(object);
}
public Object get(Class clazz, Serializable id) throws Exception{
 
  try{
  return super.getHibernateTemplate().get(clazz, id);
  }catch (Exception e) {
   e.printStackTrace();
  }
  return null;
}


public List list(final String hql, final int pageNo, final int pageSize) throws Exception{
  return super.getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query = session.createQuery(hql);
    query.setFirstResult((pageNo - 1) * pageSize);
    query.setMaxResults(pageSize);
    return query.list();
   }
  });
}

public int getRowCount(final String hql) throws Exception{
  Long i = (Long) super.getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
     Query query = session.createQuery(hql);
    return query.uniqueResult();
   }
  });
  return i.intValue();
}

public List list(String hql) throws Exception{
  return super.getHibernateTemplate().find(hql);
}

public void execute(final String hql) throws Exception{
  super.getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query = session.createQuery(hql);
    return query.executeUpdate();
   }
  });
}

public Object execHql(final String hql) throws Exception{
 
  return (Object)super.getHibernateTemplate().execute(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query = session.createQuery(hql);
    return query.uniqueResult();
   }
  });
}

public List sqllist(final String sql, final int pageNo, final int pageSize) throws Exception{
  return super.getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query = session.createSQLQuery(sql);
    query.setFirstResult((pageNo - 1) * pageSize);
    query.setMaxResults(pageSize);
    return query.list();
   }
  });
}

@Override
public List sqllist(final String sql) throws Exception{
  return super.getHibernateTemplate().executeFind(new HibernateCallback() {
   public Object doInHibernate(Session session)
     throws HibernateException, SQLException {
    Query query = session.createSQLQuery(sql);
    return query.list();
   }
  });
}
}

猜你喜欢

转载自z-gxjs.iteye.com/blog/1846400