网上示例方法,加载jsqlparser-*.jar依赖包和pagehelper-*.jar,配置mybaits配置文件,后直接在server和impl层调用 mybatis-config.xml配置: server层: PageInfo selectFile( Integer pageNo, Integer pageSize);impl层:
mybatis-config.xml配置:server层: PageInfo selectFile( Integer pageNo, Integer pageSize); impl层: public class FileServerImpl implements FileServer { ....... public PageInfo selectFile( Integer pageNo, Integer pageSize) { List list = fileMapper.selectFile(); pageNo = pageNo == null?1:pageNo; pageSize = pageSize == null?10:pageSize; PageHelper.startPage(pageNo, pageSize); //用PageInfo对结果进行包装 PageInfo page = new PageInfo (list); return page; } } junit: @Test public void testSelectFile(){ PageInfo fileDemo = fileServerImpl.selectFile(1, 3); //测试PageInfo全部属性 System.out.println("getPageNum "+fileDemo.getPageNum()); System.out.println("getPageSize "+fileDemo.getPageSize()); System.out.println("getStartRow "+fileDemo.getStartRow()); System.out.println("getEndRow "+fileDemo.getEndRow()); System.out.println("getTotal "+fileDemo.getTotal()); System.out.println("getPages "+fileDemo.getPages()); System.out.println("getFirstPage "+fileDemo.getFirstPage()); System.out.println("getLastPage "+fileDemo.getLastPage()); System.out.println("isHasPreviousPage "+fileDemo.isHasPreviousPage()); System.out.println("isHasNextPage "+fileDemo.isHasNextPage()); for(int i=0;i 改进方案 -运用泛型,上面只是一个函数,要实现分页有很多地方,不只是一个函数,也不只是一个表or对象。 添加page泛型类: @Service("PageServer") public class PageServer{ /** * @param list 查询结果 * @param pageNo 开始页码 * @param pageSize 每页显示数量 * @return */ public PageInfo getPage( List list, Integer pageNo, Integer pageSize){ pageNo = pageNo == null?1:pageNo; pageSize = pageSize == null?10:pageSize; PageHelper.startPage(pageNo, pageSize); PageInfo page = new PageInfo (list); return page; } } impl层: public class FileServerImpl implements FileServer { @Resource PageServer pageFile; ....... public PageInfo selectFile( Integer pageNo, Integer pageSize) { List list = fileMapper.selectFile(); PageInfo page= pageFile.getPage(list, pageNo, pageSize); return page; } } 其他 不变