1. 程式人生 > >sql 幾種分頁方法

sql 幾種分頁方法

action層,傳入page引數

public String findApplication(){
		Page page  = new Page();
		page.setStart(this.getStart());
		page.setLimit(this.getLimit());
		List results = service.findApplication(application, page);
		String json = this.listToGridJoson(page, results);
		this.setJsonStr(json);
		return SUCCESS;	
	}

home層,處理方式。

1.擷取

if (null != page) {
			page.setTotalCount(count(sbf.toString()));
			query.setMaxResults(page.getLimit());
			query.setFirstResult(page.getStart());
		}

	private Integer count(String hql){
		int index = hql.indexOf("FROM");
		hql = hql.substring(index);
		StringBuilder countHql = new StringBuilder("SELECT COUNT(*) ");
		countHql.append(hql);
		Query query = sessionFactory.getCurrentSession().createQuery(countHql.toString());
		List result = query.list();
		Long count = (Long)result.get(0);
		return count.intValue();
	}

2.直接計數
String sqlcount = "SELECT COUNT(*) from (select DISTINCT supplier as \"supplier\" from SUPPLIER_MANAGEMENT)";
		String a = sessionFactory.getCurrentSession().createSQLQuery(sqlcount).list().get(0).toString();
		 int dcount = Integer.parseInt(a);
		if (null != page) 
		{
			page.setTotalCount(dcount);
			query.setMaxResults(page.getLimit());
			query.setFirstResult(page.getStart());
		}