*
* @return lastpagesize int 最后一页的记录数
public int getLastPageCount() { int lastpagesize = 0; if (size % pageSize == 0) { lastpagesize = pageSize; } else { lastpagesize = size % pageSize; } return lastpagesize; } // 设置显示的记录列表 private void setShowList(List list) { ("$$totalSize=" + this.size + "; curPageNo=" + this.curPageNo + "; pageSize=" + this.pageSize + "; groupSize=" + this.groupSize); if (list == null) { list = new ArrayList(); } if (pageSize >= list.size()) { this.showList = list; } else { if (groupSize <= 1) { groupSize = 1; if (pageSize * (curPageNo + 1) > list.size()) { if (pageSize * curPageNo > list.size()) { this.showList = list.subList(list.size() - pageSize, list.size());// 返回最后一页的数据 } else { this.showList = list.subList(pageSize * curPageNo, list .size()); } } else { this.showList = list.subList(pageSize * curPageNo, pageSize * (curPageNo + 1)); } } else { if (pageSize * ((curPageNo % groupSize) + 1) > list.size()) { this.showList = list.subList(pageSize * (curPageNo % groupSize), list.size()); } else { this.showList = list.subList(pageSize * (curPageNo % groupSize), pageSize * ((curPageNo % groupSize) + 1)); }
} } public List getShowList() { return showList; } public int getCurPageNo() { return curPageNo; } public void setCurPageNo(int curPageNo) { this.curPageNo = curPageNo; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getGroupSize() { return groupSize; } public void setGroupSize(int groupSize) { this.groupSize = groupSize; } private int parseInt(Object s, int defaultValue) { if (s != null && s.toString().matches("\\d+")) { return Integer.parseInt(s.toString()); } else { return defaultValue; } } /**
* 拆解简单sql type:1-取from,2-取where,3-取orderby */ private String parseHql(String sql, int type) { switch (type) { case 1: if (sql.indexOf("where") > 0) { return sql.substring(0, sql.indexOf("where") - 1); } else if (sql.indexOf("order by") > 0) { return sql.substring(0, sql.indexOf("order by") - 1); } else { return null; } case 2: if (sql.indexOf("where") > 0) { if (sql.indexOf("order by") > 0) { return sql.substring(sql.indexOf("where"), sql .indexOf("order by") - 1); } else { return sql.substring(sql.indexOf("where")); } } else { return null; } case 3: if (sql.indexOf("order by") > 0) { return sql.substring(sql.indexOf("order by")); } else { return null; } } return null; } /** * 获取总记录条数 * * @param sql * @return */ private int getTotalCount(String sql) { log.debug("query sql:" + sql); String from = parseHql(sql, 1); String where = parseHql(sql, 2); log.debug("parse sql result - from:" + from); log.debug("parse sql result - where:" + where);
log.error(">sql 无效:" + sql); return 0; } if (where == null) where = ""; try { String fromTrim = from.substring(from.indexOf("from")); String s = "select count(1) " + fromTrim + " " + where; log.debug("get total count sql:" + s); return getSpringJdbcTemplate().queryForInt(s); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 通过sql查询一页的数据 * * @param sql * @param firstResult * @param maxResults * @return */ private List getPageData(final String sql, final int firstResult, final int maxResults) { log.debug("$$sql=" + sql + "; firstResult=" + firstResult + "; maxResults=" + maxResults); try { JdbcTemplate jt = getSpringJdbcTemplate(); return queryForList(sql + " limit " + firstResult + "," + maxResults, jt); } catch (Exception e) { e.printStackTrace(); } return new ArrayList(); } public static List queryForList(String sql, JdbcTemplate jt) { log.debug("SpringJdbcTemplate:" + sql); final List list = new ArrayList(); try { jt.query(sql, new RowCallbackHandler() { public void processRow(ResultSet rs) throws SQLException { HashMap m = new HashMap();
} if (meta != null) { int colCount = meta.getColumnCount(); for (int i = 1; i <= colCount; i++) { String fieldName = meta.getColumnName(i); Object fieldValue = rs.getString(fieldName); m.put(fieldName, fieldValue); fieldName = null; fieldValue = null; } } list.add(m); m = null; meta = null; } }); } catch (Exception e) { e.printStackTrace(); } return list; } /** * 需要实现此方法,返回一个SpringJdbcTemplate对象 */ private JdbcTemplate getSpringJdbcTemplate() { //TODO return null; }