直接来上一段代码:
接口
public <E> List<E> queryList(String sqlId, int offset, int number) throws DaoException;
实现类:
@Override public <E> List<E> queryList(String sqlId, int offset, int limit) throws DaoException { try { return sqlSessionTemplate.selectList(sqlId, new RowBounds(offset, limit)); } catch (Throwable e) { throw new DaoException(e.getMessage(), e); } }
映射文件这样写
<select id="queryList" parameterType="" resultMap=""> select <include refid="Base_Column_List"/> from tabkename a WHERE 1 = 1 limit #{offset}, #{limit} </select>
再来个分页的工具参数类
public class PageBean<T> implements Serializable { private Integer pageNum ; private Integer pageSize = 10; private Integer totalNum; private Integer totalPage; private Integer lastPage; private Integer startIndex = 1; private Integer endIndex; private Integer nextPage; private List<T> list;
get() set()方法...
}
pageBean = new PageBean<>(); pageBean.setPageNum(pageNum); pageBean.setPageSize(pageSize);
/*总页数计算*/
double totalPage = Math.ceil((int)totalCount / pageSize);
pageBean.setTotalPage((int) totalPage);
/*起始索引计算*/
pageBean.setStartIndex((pageNum - 1) * pageSize);
/*上一页下一页计算*/
pageBean.setNextPage(pageNum+1);
pageBean.setLastPage(pageNum-1);
ok,基本完成,核心的是RowBounts的offset和limit的两个参数