赞
踩
利用分页插件实现分页,同时将分页信息返回给前端,供前端进行分页数据的显示
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
添加在<configuration></configuration>
标签内
<!-- 分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
public void test(){ try { InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); SqlSession sqlSession = sqlSessionFactory.openSession(true); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); //查询之前开启分页功能 //pageNum: 当前页码 //pageSize: 页面数据条数 PageHelper.startPage(1,5); //查询数据 List<Emp> list = mapper.selectByExample(null); //获取分页信息 //list为查询出来的数据,navigatePages为导航页条数 PageInfo<Emp> pageInfo = new PageInfo<>(list,5); System.out.println(pageInfo); } catch (IOException e) { throw new RuntimeException(e); } }
返回数据如下:
* PageInfo{pageNum=1, pageSize=5, size=5, startRow=1, endRow=5, total=54, pages=11,
* list=Page{count=true, pageNum=1, pageSize=5, startRow=0, endRow=5, total=54, pages=11, reasonable=false, pageSizeZero=false}[Emp{eid=1, empName='cx', age=18, sex=1, did=1}, Emp{eid=2, empName='cxx', age=19, sex=1, did=1}, Emp{eid=3, empName='jared', age=20, sex=1, did=1}, Emp{eid=4, empName='a', age=null, sex=null, did=null}, Emp{eid=5, empName='0', age=0, sex=1, did=1}],
* prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=5, navigatepageNums=[1, 2, 3, 4, 5]}
*/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。