场景需要搜索的数据在第一页,现在翻到第二页进行搜索。此时搜索不出来结果。调试代码发现有问题。我的解决方式是重写翻页实现。
数据库是mysql,发现分页有问题的地方是在Model.java的这个方法里面
Page<> (Config configConnection connpageNumberpageSizeBoolean isGroupBySqlString selectString sqlExceptSelectObject... paras) Exception { (pageNumber < || pageSize < ) { ActiveRecordException()} (config..isTakeOverModelPaginate()) { config..takeOverModelPaginate(conngetUsefulClass()pageNumberpageSizeisGroupBySqlselectsqlExceptSelectparas)} String totalRowSql = + config..replaceOrderBy(sqlExceptSelect)List result = Db.(configconntotalRowSqlparas)size = result.size()(isGroupBySql == ) { isGroupBySql = size > } totalRow(isGroupBySql) { totalRow = size} { totalRow = (size > ) ? ((Number)result.get()).longValue() : } (totalRow == ) { Page<>(ArrayList<>()pageNumberpageSize)} totalPage = () (totalRow / pageSize)(totalRow % pageSize != ) { totalPage++} (pageNumber > totalPage) { Page<>(ArrayList<>()pageNumberpageSizetotalPage()totalRow)} String sql = config..forPaginate(pageNumberpageSizeselectsqlExceptSelect)List<> list = find(connsqlparas)Page<>(listpageNumberpageSizetotalPage()totalRow)}
我在BaseProjectModel.java重写的,代码以及注释如下
/**
* 重写分页 解决 当前页数 大于 总页数时 的bug
*
* @param paginator the page
* @param select the select part of the sql statement
* @param sqlExceptSelect the sql statement excluded select part
* @param paras the parameters of sql
* @return
*/
@Override
public Page<M> paginate(Paginator paginator, String select, String sqlExceptSelect, Object... paras) {
Config config = getConfig();
int pageNumber = paginator.getPageNo();
int pageSize = paginator.getPageSize();
if (pageNumber < 1 || pageSize < 1) {
throw new ActiveRecordException("pageNumber and pageSize must more than 0");
}
String totalRowSql = "select count(*) " + config.getDialect().replaceOrderBy(sqlExceptSelect);
List result = Db.query(totalRowSql, paras);
int size = result.size();
long totalRow;
if (false) {
totalRow = size;
} else {
totalRow = (size > 0) ? ((Number) result.get(0)).longValue() : 0;
}
if (totalRow == 0) {
// totalRow = 0;
return new Page<M>(new ArrayList<M>(0), pageNumber, pageSize, 0, 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);
//当前页数 大于总页数的时候 把当前页设置为第1页
pageNumber = 1;
}
String sql = config.getDialect().forPaginate(pageNumber, pageSize, select, sqlExceptSelect);
List<M> list = super.find(sql, paras);
return new Page<M>(list, pageNumber, pageSize, totalPage, (int) totalRow);
}
以上就是本人实际使用中发现的jfinal关于分页的bug反馈和解决办法。
用法
Page<TbArticle> page = TbArticle..paginate(getPaginator()sql.toString())setAttr(page)