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 {
/**
* <b>function:</b> 暴露HibernateTemplate模板,当基类(增删改查组件)方法不够用可以用模板进行操作
*
* @return HibernateTemplate
*/
public HibernateTemplate getHibernateTemplate();
public <T> Serializable save(T entity) throws Exception;
public <T> boolean update(T entity) throws Exception;
public <T> void saveOrUpdate(T entity) throws Exception;
public <T> boolean delete(T entity) throws Exception;
public <T> boolean deleteAll(Collection<T> entities) throws Exception;
public <T> T get(Class<T> c, Serializable id) throws Exception;
public <T> 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 <T> List<T> findByHql(String hql, Object... args) throws Exception;
public <T> List<T> findBySql(String sql) throws Exception;
public <T> List<T> findAll(Class<T> c) throws Exception;
/**
* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、当前页数、每页显示调试数;返回查询后的list集合; list集合保存总记录调试和记录结果
*
* @param queryHql
* 查询记录hql语句
* @param queryCountHql
* 查询记录条数hql语句
* @param firstResult
* 当前查询页
* @param maxResult
* 每页显示多少条
* @return List返回集合 集合0保存查询结果、集合1保存总记录条数
* @throws Exception
*/
public <T> List<T> queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception;
/**
* <b>function:</b> 传入查询语句和查询总条数(总记录)的hql语句、page分页对象;返回查询后的list集合;
*
* @createDate 2010-8-3 上午11:16:59
* @author hoojo
* @param queryHql
* list集合结果查询
* @param queryCountHql
* 总记录调试查询
* @param page
* 分页对象
* @throws Exception
*/
public <T> void queryForPage(String queryHql, String queryCountHql, Page<T> page, Object... args) throws Exception;
/**
* <b>function:</b> 分页查询,传入查询count的hql语句和DetachedCriteria动态查询条件进行查询分页
*
* @param queryCountHql
* hql查询count语句总条数
* @param cResult
* DetachedCriteria 动态查询条件
* @param firstResult
* 起始
* @param maxResult
* 最大页数
* @return List<?> 查询集合
* @throws Exception
*/
public <T> List<T> queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception;
/**
* <b>function:</b> 分页查询,传入查询的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 <T> void queryForPage(String queryCountHql, DetachedCriteria cResult, Page<T> page) throws Exception;
/**
* <b>function:</b> 传入查询条件DetachedCriteria进行查询
*
* @createDate 2010-8-3 上午11:55:28
* @author hoojo
* @param <T>
* 类型
* @param dc
* DetachedCriteria动态条件查询
* @return List
* @throws Exception
*/
public <T> List<T> 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 <T> Serializable save(T entity) throws Exception {
return this.getHibernateTemplate().save(entity);
}
@Override
public <T> 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 <T> List<T> findBySql(String sql) throws Exception {
List<T> list = null;
try {
list = (List<T>) this.getSession().createSQLQuery(sql).list();
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
@Override
public <T> 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 <T> 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> T get(Class<T> 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> 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 <T> List<T> findByHql(String hql, Object... args) throws Exception {
List<T> list = null;
try {
list = this.getHibernateTemplate().find(hql, args);
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> findAll(Class<T> c) throws Exception {
List<T> list = null;
try {
list = (List<T>) this.getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(c));
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> queryForPage(String queryHql, String queryCountHql, int firstResult, int maxResult, Object... args) throws Exception {
List<T> list = new ArrayList<T>();
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 <T> void queryForPage(String queryHql, String queryCountHql, Page<T> 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 <T> List<T> queryForPage(String queryCountHql, DetachedCriteria cResult, int firstResult, int maxResult) throws Exception {
List<T> list = new ArrayList<T>();
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 <T> List<T> find(DetachedCriteria dc) throws Exception {
List<T> list = new ArrayList<T>();
try {
list = (List<T>) this.getHibernateTemplate().findByCriteria(dc);
} catch (Exception e) {
throw new RuntimeException(e);
}
return list;
}
@SuppressWarnings("unchecked")
@Override
public <T> void queryForPage(String queryCountHql, DetachedCriteria cResult, Page<T> 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 <T> boolean deleteAll(Collection<T> entities) throws Exception {
boolean bo = false;
try {
this.getHibernateTemplate().deleteAll(entities);
bo = true;
} catch (Exception e) {
bo = false;
throw new RuntimeException();
}
return bo;
}
}