QBC分页查询

1、第一种方式

public class DetailDaoImpl extends HibernateTemplate implements DetailDaoInterface {

  private Log log=LogFactory.getLog(ConstantsApplication.P2PAPPLICATION_LOG);

/**
 * 查询符合条件的指定区间的记录
 */
 public List<AppInfoPO> queryApproveDetailInfo(ApproveDetailQueryModel qm, int startNumber, int maxNumber) throws APSException {

  log.info("开始查询明细……");
    try{

    DetachedCriteria c = this.getDetachedCriteria(qm);
      return  findByCriteria(c,startNumber,maxNumber);

    } catch (Exception e) {
         log.error("查询明细异常", e);
         throw new APSException("查询明细异常,在方法queryApproveDetailInfo中", e);
     }

}

/**
  * 查询符合条件的记录数
  */
 public int queryApproveDetailInfoCount(ApproveDetailQueryModel qm) throws APSException {

  log.info("开始查询审批明细……");
    DetachedCriteria d = this.getDetachedCriteria(qm);
    d.setProjection(Projections.rowCount());
    List list = this.findByCriteria(d);
    if (list != null && list.size() > 0)
     return Integer.valueOf(list.get(0).toString());
    return 0;

}

/**
 * 创建公共查询条件
 * @param qm
 * @return
 */
 public DetachedCriteria  getDetachedCriteria(ApproveDetailQueryModel qm){
      DetachedCriteria c = DetachedCriteria.forClass(AppInfoPO.class);
          if(!CommonMethod.isNull(qm.getCreditDateisnotnull())){
           c.add(Property.forName("creditDate").isNotNull());
          }

    c.addOrder(Order.asc("creditName"));
      c.addOrder(Order.desc("creditDate")); 
      return c;

}

2、SQL分页

Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List list = query.list();

 

hxzon

public List getCategorys(final int currentpage, final int pagesize) {
log.debug("finding all ShbbsCategory instances");

   List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from ShbbsCategory";
Query query = session.createQuery(hql);
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);
List list = query.list();
return list;
}
});
return list;

}

预编译

String hql = "FROM ShbbsCategory AS p WHERE p.uid LIKE ? OR p.name LIKE ?";
Query query = session.createQuery(hql);
query.setString(0, "%" + cond + "%");
query.setString(1, "%" + cond + "%");
query.setFirstResult((currentpage - 1) * pagesize);
query.setMaxResults(pagesize);

集合总数1

public Integer getSaleChanceListCount() {    

  final String hql = "select count(*) from SaleChanceInfo si";    

   Query query = arg0.createQuery(hql);    

  return query.uniqueResult();    

}

集合总数2

String hql = "SELECT COUNT(p.id) from Person AS p WHERE p.uid LIKE ? OR p.name LIKE ?";

Query query = session.createQuery(hql);

List list = query.list();

  if (list.size() > 0) {
count = (Integer) list.get(0);
}
return count;

3、通过Criteria实现分页

Criteria criteria = session.createCriteria( Class arg);

criteria.setFirstResult(offset);
criteria.setMaxResults(length);

return criteria.list();

 

你可能感兴趣的:(分页查询)