WebmailHibernateBaseDAOImpl.java 6.5 KB
package com.espeed.dao.impl;
 
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;

import com.espeed.dao.HibernateBaseDAO;
import com.espeed.vo.PageBean;

/**
 * 程序名称:    	EspeedMail_时速邮箱
 * 程序版本:    	V1.0
 * 作    者:    	深圳市科飞时速网络技术有限公司(0755-88843776)
 * 版权所有:    	深圳市科飞时速网络技术有限公司
 * 技术支持:    	Tech@21gmail.com
 * 单元名称:    通用dao操作接口实现类
 * 开始时间:    	2013.09.22
 * 程 序 员:    	谢勇
 * 最后修改:    2014-02-26
 * 备    注:	实现通用的增删改查操作,被其他dao所继承使用
 */

public abstract class WebmailHibernateBaseDAOImpl<T extends Serializable ,Pk extends Serializable> extends HibernateDaoSupport
	implements HibernateBaseDAO<T, Pk> {
	/**
	 * ------------------------------常用CRUD操作-----------------------------------------
	 */
	/**增加记录 (返回新增加记录的主键)*/
	public int  add(T o) throws Exception {
		return (Integer) super.getHibernateTemplate().save(o);	
	}

	/**增加记录(无返回值)*/
	public void  adddate(T o) throws Exception {
		super.getHibernateTemplate().save(o);			
	}

	 /**修改记录*/
	public void update(T o) throws Exception {
		super.getHibernateTemplate().update(o);
	}
	
	/**删除记录*/
	public void del(T o) throws Exception {
		super.getHibernateTemplate().delete(o);
	}

	/**添加或更新*/
	public void saveOrUpdate(T o) throws Exception {
		super.getHibernateTemplate().saveOrUpdate(o);
	}
	
	/**根据ID获取一条数据*/
	@SuppressWarnings("unchecked")
	public T get(Class<T> t,Pk pk) throws Exception {
		return (T) super.getHibernateTemplate().get(t, pk);
	}
	
	/**根据ID获取一条数据*/
	@SuppressWarnings("unchecked")
	public T load(Class<T> t,Pk pk) throws Exception {
		return (T) super.getHibernateTemplate().load(t, pk);
	}
	
	/**根据hql进行条件查询*/
	@SuppressWarnings("unchecked")
	public List<T> getAll(String hql) throws Exception {
		return super.getHibernateTemplate().find(hql);
	}
	
	/**条件查询*/
	@SuppressWarnings("unchecked")
	public List<T> getAll(String whereHql, Object... params) throws Exception {
		return super.getHibernateTemplate().find(whereHql,params);
	}
	
	/**
	 * ------------------------------分页查询-----------------------------------------
	 */
    /** 根据指定的HQL(多条件)进行查询*/
	public List<T> findByPage(String hql, String hqlCount, PageBean pb)
			throws Exception {
		return this.findByPage(hql, hqlCount, pb, new Object[0]);
	}
	/** 根据指定的HQL(多条件)进行查询*/
	@SuppressWarnings("unchecked")
	public List<T> findByPage(final String hql,final String hqlCount,final PageBean pb,final Object... values)
			throws Exception {
		return (List<T>) super.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				int pageSize = pb.getPageSize();
				int currentPage = pb.getCurrentPage();
				Query queryCount = session.createQuery(hqlCount);
				Query query = session.createQuery(hql);
				for(int i = 0;i<values.length;i++){
					queryCount.setParameter(i, values[i]);
					query.setParameter(i, values[i]);
				}
				//获取总记录数
				Integer totalRecord = Integer.valueOf(queryCount.list().get(0).toString());
				pb.setTotalRecord(totalRecord);
				//获取总页数
				Integer totalPage = (totalRecord + pageSize - 1)/pageSize;
				pb.setTotalPage(totalPage);
				query.setFirstResult((currentPage - 1)*pageSize);
				query.setMaxResults(pageSize);
				List list = query.list();
				return query.list();
			}
			
		});
	}
	
	/*
	 * -------------------------------查询总记录数-----------------------------------------
	 */
	/**根据条件查询总记录*/
	public int count(String hql) throws Exception {
		List result=super.getHibernateTemplate().find(hql);
		if(result.size()>0&&result.get(0)!=null&&!result.get(0).equals("null")){
			return Long.valueOf(result.get(0).toString()).intValue();
			//return Integer.valueOf(result.get(0).toString());
		}else{
			return 0;
		}
		//return Integer.valueOf(super.getHibernateTemplate().find(hql).get(0).toString());
	}
	
	/**根据条件和参数查询总记录*/
	public int count(String hql, Object... params) throws Exception {
		return Integer.valueOf(super.getHibernateTemplate().find(hql,params).get(0).toString());
	}
	
	/**HQL语句修改删除记录*/
	public int updateorDelBySql(String hql){
		Query query =	null;
		int result=0;		
		Session session =null;
		session=super.getHibernateTemplate().getSessionFactory().openSession();		
		query = session.createQuery(hql);
		result = query.executeUpdate();	
		return result;
	}
	
	/**sql语句修改删除记录*/
	public int updateorDelSql(String sql){
		Query query =	null;
		int result=0;		
		Session session =null;
		try {
			session=super.getHibernateTemplate().getSessionFactory().openSession();		
			query = session.createSQLQuery(sql);
			result = query.executeUpdate();	
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.clear();
				session.close();
			}
		}
		
		return result;
	}
	
	/**查询设定的记录条数据*/
	public List<T> findBySet(String hql,int num){
		Session session =	null;	
		Query query=null;
		try {
			session=super.getHibernateTemplate().getSessionFactory().openSession();	
			query = session.createQuery(hql);
			query.setMaxResults(num);
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.clear();
				session.close();
			}
		}		
		return null;
	}
	
	/**批量插入*/
	public void addPi(List<T> o)throws Exception{
		Session session =	super.getHibernateTemplate().getSessionFactory().openSession();			
		if(o!=null){				
			session.beginTransaction();
			for(int i=0;i<o.size();i++){
				session.saveOrUpdate(o.get(i));	
			}
			session.beginTransaction().commit();
		}
	}
	
	/**sql语句查询
	 * @throws Exception */
	public List findBySql(String sql) throws Exception{
		Session session=null;
		try {
			session =super.getHibernateTemplate().getSessionFactory().openSession();
			return session.createSQLQuery(sql).list();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.clear();
				session.close();
			}
		}
		return null;		
	}
}