com.jfinal.plugin.activerecord.Model 增加代码: //------------------------------------------------------------------------------------------------------------------ /** * Paginate. * @param pageNumber the page number * @param pageSize the page size * @param sql the select part of the sql statement * @param paras the parameters of sql * @return the Page object */ public Page<M> paginate(int pageNumber, int pageSize, String sql, Object... paras) { Config config = getConfig(); Connection conn = null; try { conn = config.getConnection(); return doPaginate(config, conn, pageNumber, pageSize, sql, paras); } catch (Exception e) { throw new ActiveRecordException(e); } finally { config.close(conn); } } private Page<M> doPaginate(Config config, Connection conn, int pageNumber, int pageSize, String select, Object... paras) throws Exception { if (pageNumber < 1 || pageSize < 1) { throw new ActiveRecordException("pageNumber and pageSize must more than 0"); } String totalRowSql = "select count(*) from (" + config.dialect.replaceOrderBy(select)+ " ) as __tmp"; List result = Db.query(config, conn, totalRowSql, paras); int size = result.size(); long totalRow = (size > 0) ? ((Number)result.get(0)).longValue() : 0; if (totalRow == 0) { return new Page<M>(new ArrayList<M>(0), pageNumber, pageSize, 0, 0); // totalRow = 0; } int totalPage = (int) (totalRow / pageSize); if (totalRow % pageSize != 0) { totalPage++; } if (pageNumber > totalPage) { return new Page<M>(new ArrayList<M>(0), pageNumber, pageSize, totalPage, (int)totalRow); } String sql = config.dialect.forPaginate(pageNumber, pageSize, select,""); List<M> list = find(conn, sql, paras); return new Page<M>(list, pageNumber, pageSize, totalPage, (int)totalRow); } /** * Paginate. * @param pageNumber the page number * @param pageSize the page size * @param sql the select part of the sql statement * @return the Page object */ public Page<M> paginate(int pageNumber, int pageSize, String sql) { return paginate(pageNumber,pageSize,sql,(Object[])null); } //------------------------------------------------------------------------------------------------------------------