@Override public Pager query(Pager pager, WeeklySalesPO po) { String condition = this.queryCondition(QUERY_SQL, po); StringBuilder sql = new StringBuilder(pagerAdapter(pager, condition)); List<WeeklySalesPO> list = this.getJdbcTemplate().query(sql.toString(), queryMapper); log.debug("query with page"); pager.setList(list); return pager; }
protected String pagerAdapter(Pager pager, String condition) { pager.setCurrentPage(pager.getCurrentPage()); pager.setSize(pager.getSize()); StringBuilder totalSql = new StringBuilder("SELECT COUNT(*) AS COUNT FROM (").append(condition).append(")"); pager.setTotal(this.getJdbcTemplate().queryForInt(totalSql.toString())); int end = 0; int start = (pager.getCurrentPage() - 1) * pager.getSize(); if (start == pager.getSize()) { start = (pager.getCurrentPage() - 1) * pager.getSize() + 1; end = (pager.getCurrentPage() - 1) * pager.getSize() + pager.getSize(); } else if (start == 0) { end = pager.getSize(); } else { end = (pager.getCurrentPage() - 1) * pager.getSize() + pager.getSize() - 1; } StringBuilder sql = new StringBuilder("SELECT * FROM (SELECT A.*, ROWNUM RN FROM ( "); sql.append(condition); sql.append(" ) A WHERE ROWNUM <= "); sql.append(end); sql.append(" ) WHERE RN >= "); sql.append(start); return sql.toString(); }