public class Pager extends HibernateDaoSupport {
/** 日志加载* */
private static final Log log = LogFactory.getLog(Pager.class);
/**
* 父类初始化方法
*/
protected void initDao() {
// do nothing
}
/**
* 单个对象分页
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPage(String className, PageHelper pageHelper)
throws Exception {
log.debug("finding Object instance by page");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page failed", e);
throw e;
}
}
/**
* 单个对象分页,单个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrder(String className, PageHelper pageHelper,
String propertyName, boolean ascending) throws Exception {
log.debug("finding Object instance by page and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className)).addOrder(
ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and order failed", e);
throw e;
}
}
/**
* 单个对象分页,多个属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param orders
* Map名key为属性名,value为是否正序排名
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndOrders(String className, PageHelper pageHelper,
Map<String, Boolean> orders) throws Exception {
log.debug("finding Object instance by page and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCount(className));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,sql查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhere(String className, PageHelper pageHelper,
String where) throws Exception {
log.debug("finding Object instance by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExample(String className,
PageHelper pageHelper, Object example) throws Exception {
log.debug("finding Object instance by page and example");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 单个对象分页,�?��对象属性条件查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param propertyName
* 要排序的属性名
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrder(String className,
PageHelper pageHelper, Object example, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and example and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and order failed", e);
throw e;
}
}
/**
* 单个对象分页,单个对象属性条件查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param example
* Object对象,与classsName对应的对象
* @param orders
* Map对象,key为要排序的属性名,value为是否正序排序
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndExampleAndOrders(String className,
PageHelper pageHelper, Object example, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and example and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByExample(className,
pageHelper, example));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and example and orders failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,单属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param propertyName
* 要排序的属性名称
* @param ascending
* 是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrder(String className,
PageHelper pageHelper, String where, String propertyName,
boolean ascending) throws Exception {
log.debug("finding Object instance by page and sql and order");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
c.addOrder(ascending ? Order.asc(propertyName) : Order
.desc(propertyName));
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and order failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录
*
* @param pageHelper
* @param queryString
* @return List
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndSql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Query query = this.getSession().createQuery(queryString);
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountBySql(pageHelper,
queryString));
}
query.setFirstResult(firstResult);
query.setMaxResults(maxResults);
return query.list();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 单个对象分页,hql查询,多属性排序
*
* @param className
* 对象名
* @param pageHelper
* 分页属性
* @param where
* where条件
* @param orders
* Map对象,key要排序的属性名称,value是否正序排列
* @return List
* @throws Exception
* @since Jul 17, 2009
*/
@SuppressWarnings("unchecked")
public List findAllByPageAndWhereAndOrders(String className,
PageHelper pageHelper, String where, Map<String, Boolean> orders)
throws Exception {
log.debug("finding Object instance by page and sql and orders");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findAllCountByWhere(className,
where));
}
if (orders != null && !orders.isEmpty()) {
Iterator ordersItr = orders.keySet().iterator();
while (ordersItr.hasNext()) {
String propertyName = ordersItr.next().toString();
boolean ascending = orders.get(propertyName);
if (ascending) {
c.addOrder(Order.asc(propertyName));
} else {
c.addOrder(Order.desc(propertyName));
}
}
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Objcet by page and sql and orders failed", e);
throw e;
}
}
/**
* 条件查询分页
*
* @param pageHelper
* 分页属性
* @param c
* Criteria
* @return List
* @throws Exception
* @since Jul 14, 2009
*/
@SuppressWarnings("unchecked")
public List findByPageAndCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
log.debug("finding Object instance by page and criteria");
try {
int firstResult = pageHelper.getFirstResult();
int maxResults = pageHelper.getMaxResults();
if (pageHelper.isAutoCount()) {
pageHelper.setTotalRows(this.findCountByCriteria(pageHelper, c));
}
c.setFirstResult(firstResult);
c.setMaxResults(maxResults);
return c.list();
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 查询记录总数
*
* @param className
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCount(String className) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find all count failed", e);
throw e;
}
}
/**
* 根据where条件查询记录总数
*
* @param className
* @param where
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByWhere(String className, String where)
throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Restrictions.sqlRestriction(where));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据对象查询记录总数
*
* @param className
* @param pageHelper
* @param example
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountByExample(String className, PageHelper pageHelper,
Object example) throws Exception {
try {
Criteria c = this.getSession().createCriteria(
Class.forName(className));
c.add(Example.create(Class.forName(className).cast(example)));
c.setProjection(Projections.rowCount());
return Integer.parseInt(c.uniqueResult().toString());
} catch (Exception e) {
log.error("find Objcet by page and example failed", e);
throw e;
}
}
/**
* 根据hql语句查询记录总数
*
* @param pageHelper
* @param queryString
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
public int findAllCountBySql(PageHelper pageHelper, String queryString)
throws Exception {
log.debug("finding Object by page and sql");
try {
Query query = this.getSession().createQuery(queryString);
return query.list().size();
} catch (Exception e) {
log.error("find Objcet by page and sql failed", e);
throw e;
}
}
/**
* 根据Criteria查询记录总数
* @param pageHelper
* @param c
* @return int
* @throws Exception
* @since Feb 26, 2010
*/
@SuppressWarnings("unchecked")
public int findCountByCriteria(PageHelper pageHelper, Criteria c)
throws Exception {
try {
CriteriaImpl impl = (CriteriaImpl) c;
// 先把Projection、ResultTransformer、OrderBy取出�?清空三�?后再执行Count操作
Projection projection = impl.getProjection();
ResultTransformer transformer = impl.getResultTransformer();
List<CriteriaImpl.OrderEntry> orderEntries = null;
try {
orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
// 执行Count查询
int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
// 将之前的Projection,ResultTransformer和OrderBy条件重新设回
c.setProjection(projection);
if (projection == null) {
c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
if (transformer != null) {
c.setResultTransformer(transformer);
}
try {
ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
} catch (Exception e) {
log.error("不可能抛出的异常:{}", e);
}
return totalCount;
} catch (Exception e) {
log.error("find Object by page and criteria failed", e);
throw e;
}
}
/**
* 获取分页session中的Criteria对象
*
* @param className
* @return Criteria
* @since Jul 21, 2009
*/
public Criteria getCriteria(String className) {
log.debug("getting Criteria");
try {
return this.getSession().createCriteria(Class.forName(className));
} catch (DataAccessResourceFailureException e) {
log.error("get Criteria failed", e);
return null;
} catch (IllegalStateException e) {
log.error("get Criteria failed", e);
return null;
} catch (ClassNotFoundException e) {
log.error("get Criteria failed", e);
return null;
}
}
/**
* 通过xml获取注入的bean
*
* @param ctx
* @return Pager
* @since Jul 14, 2009
*/
public static Pager getFromApplicationContext(ApplicationContext ctx) {
return (Pager) ctx.getBean("Pager");
}
}