java 开发 oracle分页通用方法

package com.richsum.hyoa.dao.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.richsum.hyoa.dao.GenericEntityDao;
import com.richsum.hyoa.dto.PageModel;
import com.richsum.hyoa.model.BaseModel;
import com.richsum.hyoa.model.OfficeDispatch;
import com.richsum.hyoa.web.PagerContext;

/**
* Abstract base class for a DAO
*
* @author Leo
*/
public abstract class GenericEntityDaoImpl<T extends BaseModel> extends
HibernateDaoSupport implements GenericEntityDao<T> {

/** the logger */
private static final Log LOG = LogFactory
.getLog(GenericEntityDaoImpl.class);

/**
* The class of the entity of this DAO.
*/
private Class<T> entityClass;

/**
* Construct this DAO for the supplied model class.
*
* @param entityClass
*            The model class that is persisted.
*/
public GenericEntityDaoImpl(final Class<T> entityClass) {
this.entityClass = entityClass;
}

public int countAll() {
Criteria criteria = getSession().createCriteria(entityClass);
criteria.setProjection(Projections.rowCount());
return (Integer) criteria.uniqueResult();
}

@SuppressWarnings("unchecked")
public List<T> listAll() {
LOG.debug("Get all" + entityClass.getSimpleName());
List<T> values = (List<T>) getHibernateTemplate().findByCriteria(
DetachedCriteria.forClass(entityClass));
return values;
}

@SuppressWarnings("unchecked")
public T get(Long id) {
LOG.debug("Get all" + entityClass.getSimpleName() + " with id " + id);
return (T) getHibernateTemplate().get(entityClass, id.longValue());
}

public <E> List<E> findByCriteria(String sqlStmt, Class<E> entityClass,
List args, Map map) {
SQLQuery query = this.getSession().createSQLQuery(sqlStmt);
query.setResultTransformer(Transformers.aliasToBean(entityClass));
if (args != null) {
int size = args.size();
for (int i = 0; i < size; i++) {
query.setParameter(i, args.get(i));
}
}
if (map != null) {
Iterator keyIt = map.keySet().iterator();
while (keyIt.hasNext()) {
Object key = keyIt.next();
Object val = map.get(key);
query.addScalar((String) key, (Type) val);
}
}
return (List<E>) query.list();
}

public <E> List<E> findByCriteria(String sqlStmt, Class<E> entityClass,
List args) {
return this.findByCriteria(sqlStmt, entityClass, args, null);
}

public List findByCriteriaByHQL(String sqlStmt, List args) {
Query query = this.getSession().createQuery(sqlStmt);
if (args != null) {
int size = args.size();
for (int i = 0; i < size; i++) {
query.setParameter(i, args.get(i));
}
}
return query.list();
}

public void save(T item) {
LOG.debug("save new item");
item.prePersist();
getHibernateTemplate().saveOrUpdate(item);
}

public void update(T item) {
LOG.debug("update item");
item.prePersist();
// getHibernateTemplate().merge(item);
getHibernateTemplate().update(item);
}

public void delete(T item) {
LOG.debug("update item");
getHibernateTemplate().delete(item);
}

/**
* {@inheritDoc}
*/
public void evictFromHibernate(T item) {
getHibernateTemplate().flush();
getHibernateTemplate().evict(item);
}

/**
* 分页查询
* @param jpql hql 语句
* @param page 当前页
* @param size 每页的记录数
* @return
*/
public List pageSearch(String hql, Integer pageNo, Integer pageSize) {
return this.pageSearchCriteriaByHQL(hql,null,pageNo,pageSize);
}

/**
* List条件查询
*
* @param sql
*            hql 语句
* @param argsList
*            参数集合
* @param pageNo
*            当前页
* @param pageSize
*            每页的记录数
* @return
*/
public List pageSearchCriteriaByHQL(String hql, List argsList,
Integer pageNo, Integer pageSize) {
try {
if (pageSize == 0) {
pageSize = 15;
}
Query query = this.getSession().createQuery(hql)
.setFirstResult((pageNo - 1) * pageSize)
.setMaxResults(pageSize);
if (null != argsList && argsList.size() > 0) {
int size = argsList.size();
for (int i = 0; i < size; i++) {
query.setParameter(i, argsList.get(i));
}
}
return query.list();
} catch (Exception e) {
System.out.println("List条件查询异常。");
e.printStackTrace();
}
return null;
}

/**
* 计算总条数
* @param hql 查询语句
* @return 总页数
*/
public int countPageSize(String hql, List paraList) {
int index = hql.indexOf("from");
hql = "select count(*) " + hql.substring(index);
return ((Number) this.findUniqueResult(hql, paraList)).intValue();
}

@Override
public PageModel<T> queryPageByHQL(String hql, List argsList) {
PageModel<T> pm = new PageModel<T>();
List list = pageSearchCriteriaByHQL(hql,argsList,PagerContext.getPageNo(),PagerContext.getPageSize());
pm.setDatas(list);
pm.setRecordCount(this.countPageSize(hql, argsList));
pm.setPageNo(PagerContext.getPageNo());
pm.setPageSize(PagerContext.getPageSize());
return pm;
}

@Override
public int queryCountPageSize(String hql){
int index = hql.indexOf("from");
hql = "select count(*) " + hql.substring(index);
Query query = this.getSession().createQuery(hql);
return ((Number) query.uniqueResult()).intValue();
}
@Override
public PageModel<T> queryCountPageHql(String hql){
PageModel<T> pm = new PageModel<T>();
pm.setDatas( pageSearch(hql, PagerContext.getPageNo(),PagerContext.getPageSize()));
pm.setRecordCount(queryCountPageSize(hql));
pm.setPageNo(PagerContext.getPageNo());
pm.setPageSize(PagerContext.getPageSize());
return pm;

}

@Override
public Object findUniqueResult(String hql, List paraList) {
Query query = this.getSession().createQuery(hql);
if (paraList != null) {
for (int i = 0; i < paraList.size(); i++) {
query.setParameter(i, paraList.get(i));
}
}
return query.uniqueResult();
}

@Override
public int excuteUpdateBySql(String sql, List list) {
SQLQuery query = this.getSession().createSQLQuery(sql);
if(list != null){
for(int i=0; i < list.size(); i++){
query.setParameter(i, list.get(i));
}
}
return query.executeUpdate();
}

@Override
public Object findUniqueResultBySQL(String sql, List list) {
SQLQuery query = this.getSession().createSQLQuery(sql);
if(list != null){
for(int i=0; i < list.size(); i++){
query.setParameter(i, list.get(i));
}
}
return query.uniqueResult();
}

}

你可能感兴趣的:(java,oracle)