天天看点

java 分页 查询公式_java分页查询 方法一 Query

方法一:

1 Action文件:stationByPage = this.sManager.getAndSearch(mainForm, searchVal, user);

2 manager文件:

public List getAndSearch(SForm form,String searchVal,User user){

int totalPage = 1;

int pageNum = form.getPage();

String orgId = form.getOrgId();

int count = sDAO.getNumOfStationsBySearch(orgId, searchVal, user);

form.setRecordNum(count);

String orderColum = form.getOrderColum();

if(orderColum != null && orderColum != ""){

sDAO.setOrderColum(orderColum);

sDAO.setAsc(form.getAsc());

}

totalPage = (count != 0 && count%form.getPageRowNum() == 0) ?

(count/form.getPageRowNum()) : (count/form.getPageRowNum() + 1);

form.setTotal(totalPage);

if (form.getPage() < 1) { // 默认首页

form.setPage(1);

}

if (form.getPage() > totalPage) { // 超过最大页,默认尾页

form.setPage(totalPage);

}

List result = this.sDAO.findByPageAndSearch(pageNum, orgId, searchVal, user);

if (result.isEmpty() && pageNum >1){

form.setPage(--pageNum);

return sDAO.findByPageAndSearch(pageNum, orgId, searchVal, user);

}

return result;

}

3 Dao文件:

public List findByPageAndSearch(int pageNum,String orgId,String searchVal,User user) {

try {   String hql = "from S where station_name like '%" + searchVal + "%'" ;  hql += " order by stationName";   Query query = getSession().createQuery(hql);   query.setFirstResult((pageNum-1) *PAGESIZE);   query.setMaxResults(PAGESIZE);   return query.list();      } catch (RuntimeException re) {   log.error("find by example failed", re);   throw re;  } }