Hibernate3 Template Dao

阅读更多
package com.king.stt.dao;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.king.util.hibernate.Page;

public interface IDao { 
	/**
	 * function: 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作
	 * 
	 * @return HibernateTemplate
	 */
	public HibernateTemplate getHibernateTemplate();

	public  Serializable save(T entity) throws Exception;

	public  boolean update(T entity) throws Exception;
	
	public  void saveOrUpdate(T entity) throws Exception;

	public  boolean delete(T entity) throws Exception;

	public  boolean deleteAll(Collection entities) throws Exception;

	public  T get(Class c, Serializable id) throws Exception;

	public  T get(String hql, Object... args) throws Exception;

	public int executeByHql(String hql, Object... args) throws Exception;

	public int executeBySql(String sql) throws Exception;

	public  List findByHql(String hql, Object... args) throws Exception;

	public  List findBySql(String sql) throws Exception;

	public  List findAll(Class c) throws Exception;

	/**
	 * function: 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; list集合保存总记录调试和记录结果
	 * 
	 * @param queryHql
	 *            查询记录hql语句
	 * @param queryCountHql
	 *            查询记录条数hql语句
	 * @param firstResult
	 *            当前查询页
	 * @param maxResult
	 *            每页显示多少条
	 * @return List返回集合 集合0保存查询结果、集合1保存总记录条数
	 * @throws Exception
	 */
	public  List queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception;

	/**
	 * function: 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合;
	 * 
	 * @createDate 2010-8-3 上午11:16:59
	 * @author hoojo
	 * @param queryHql
	 *            list集合结果查询
	 * @param queryCountHql
	 *            总记录调试查询
	 * @param page
	 *            分页对象
	 * @throws Exception
	 */
	public  void queryForPage(String queryHql, String queryCountHql, Page page, Object... args) throws Exception;

	/**
	 * function: 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页
	 * 
	 * @param queryCountHql
	 *            hql查询count语句总条数
	 * @param cResult
	 *            DetachedCriteria 动态查询条件
	 * @param firstResult
	 *            起始
	 * @param maxResult
	 *            最大页数
	 * @return List 查询集合
	 * @throws Exception
	 */
	public  List queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception;

	/**
	 * function: 分页查询,传入查询的count的hql语句和动态查询DetachedCriteria类及page分页entity
	 * 
	 * @createDate 2010-8-3 上午11:14:30
	 * @author hoojo
	 * @param queryCountHql
	 *            查询count语句
	 * @param cResult
	 *            DetachedCriteria 动态查询条件类
	 * @param page
	 *            Page分页实体类
	 * @throws Exception
	 */
	public  void queryForPage(String queryCountHql, DetachedCriteria cResult, Page page) throws Exception;

	/**
	 * function: 传入查询条件DetachedCriteria进行查询
	 * 
	 * @createDate 2010-8-3 上午11:55:28
	 * @author hoojo
	 * @param 
	 *            类型
	 * @param dc
	 *            DetachedCriteria动态条件查询
	 * @return List
	 * @throws Exception
	 */
	public  List find(DetachedCriteria dc) throws Exception;

}

 

DaoImpl

package com.king.stt.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.king.util.hibernate.Page;

@Repository(value = "dao")
public class DaoImpl extends HibernateDaoSupport implements IDao {

	@Resource
	public void setSessionFacotry(SessionFactory sessionFacotry) {
		super.setSessionFactory(sessionFacotry);
	}

	@Override
	public  Serializable save(T entity) throws Exception {
		return this.getHibernateTemplate().save(entity);
	}
	
	@Override
	public  void saveOrUpdate(T entity) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(entity);
	}

	@Override
	public int executeByHql(String hql, Object... args) throws Exception {
		return this.getHibernateTemplate().bulkUpdate(hql, args);
	}

	@Override
	public int executeBySql(String sql) throws Exception {
		return this.getSession().createSQLQuery(sql).executeUpdate();
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List findBySql(String sql) throws Exception {
		List list = null;
		try {
			list = (List) this.getSession().createSQLQuery(sql).list();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public  boolean update(T entity) throws Exception {
		boolean bo = false;
		try {
			this.getHibernateTemplate().update(entity);
			bo = true;
		} catch (Exception e) {
			bo = false;
			throw new RuntimeException(e);
		}
		return bo;
	}

	@Override
	public  boolean delete(T entity) throws Exception {
		boolean bo = false;
		try {
			this.getHibernateTemplate().delete(entity);
			bo = true;
		} catch (Exception e) {
			bo = false;
			throw new RuntimeException(e);
		}
		return bo;
	}

	@Override
	public  T get(Class c, Serializable id) throws Exception {
		T ety = null;
		try {
			ety = (T) this.getHibernateTemplate().get(c, id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return ety;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T get(String hql, Object... args) throws Exception {
		T ety = null;
		try {
			Query query = this.getSession().createSQLQuery(hql);
			if (args != null) {
				for (int i = 0; i < args.length; i++) {
					query.setParameter(i, args[i]);
				}
			}
			ety = (T) query.setMaxResults(1).uniqueResult();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return ety;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List findByHql(String hql, Object... args) throws Exception {
		List list = null;
		try {
			list = this.getHibernateTemplate().find(hql, args);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List findAll(Class c) throws Exception {
		List list = null;
		try {
			list = (List) this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception {
		List list = new ArrayList();
		try {
			Session session = this.getSession();
			Query query = session.createQuery(queryHql);
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
			list.add((T) query.setFirstResult(firstResult).setMaxResults(maxResult).list());
			Query count = session.createQuery(queryCountHql);
			for (int i = 0; i < args.length; i++) {
				count.setParameter(i, args[i]);
			}
			list.add((T) count.setMaxResults(1).uniqueResult());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  void queryForPage(String queryHql, String queryCountHql, Page page, Object... args) throws Exception {
		try {
			Session session = this.getSession();
			Query query = session.createQuery(queryHql);
			for (int i = 0; i < args.length; i++) {
				query.setParameter(i, args[i]);
			}
			page.setResult(query.setFirstResult(page.getCurrentPage()).setMaxResults(page.getPageSize()).list());
			Query count = session.createQuery(queryCountHql);
			for (int i = 0; i < args.length; i++) {
				count.setParameter(i, args[i]);
			}
			page.setTotalsCount(Integer.parseInt(count.setMaxResults(1).uniqueResult().toString()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception {
		List list = new ArrayList();
		try {
			Session session = this.getSession();
			list.add((T) this.getHibernateTemplate().findByCriteria(cResult, firstResult, maxResult));
			list.add((T) session.createQuery(queryCountHql).setMaxResults(1).uniqueResult());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List find(DetachedCriteria dc) throws Exception {
		List list = new ArrayList();
		try {
			list = (List) this.getHibernateTemplate().findByCriteria(dc);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  void queryForPage(String queryCountHql, DetachedCriteria cResult, Page page) throws Exception {
		try {
			Session session = this.getSession();
			page.setResult(this.getHibernateTemplate().findByCriteria(cResult, page.getCurrentPage(), page.getPageSize()));
			page.setTotalsCount(Integer.parseInt(session.createQuery(queryCountHql).setMaxResults(1).uniqueResult().toString()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public  boolean deleteAll(Collection entities) throws Exception {
		boolean bo = false;
		try {
			this.getHibernateTemplate().deleteAll(entities);
			bo = true;
		} catch (Exception e) {
			bo = false;
			throw new RuntimeException();
		}
		return bo;
	}

}

 

你可能感兴趣的:(Hibernate3 Template Dao)