长沙做网站建设价格,青海网站制作的公司,c qq 互联网站开发代码,网销1.limit分页
limit分页原理 mysql的limit后面两个数字#xff1a; 第一个数字#xff1a;startIndex#xff08;起始下标。下标从0开始。#xff09; 第二个数字#xff1a;pageSize#xff08;每页显示的记录条数#xff09; 假设已知页码pageNum#xff0c;还有每页…1.limit分页
limit分页原理 mysql的limit后面两个数字 第一个数字startIndex起始下标。下标从0开始。 第二个数字pageSize每页显示的记录条数 假设已知页码pageNum还有每页显示的记录条数pageSize第一个数字 startIndex (pageNum - 1) * pageSize 所以标准通用的mysql分页SQL
select *
from tableName ......
limit (pageNum - 1) * pageSize, pageSize2. PageHelper插件
使用PageHelper插件进行分页更加的便捷。 第一步引入依赖
dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.3.1/version
/dependency第二步在mybatis-config.xml文件中配置插件
!--mybatis分页的拦截器--
pluginsplugin interceptorcom.github.pagehelper.PageInterceptor/plugin
/plugins第三步编写Java代码
关键点
在查询语句之前开启分页功能。在查询语句之后封装PageInfo对象。PageInfo对象将来会存储到request域当中。在页面上展示。
Test
public void testSelectAll(){SqlSession sqlSession SqlSessionUtil.openSession();CarMapper mapper sqlSession.getMapper(CarMapper.class);// 在执行DQL语句之前开启分页功能//查询第二页前三条记录int pageNum 2;int pageSize 3;PageHelper.startPage(pageNum, pageSize);ListCar cars mapper.selectAll();cars.forEach(car - System.out.println(car));// 封装分页信息对象new PageInfo()// new PageInfo是PageHelper插件提供的用来封装分页相关信息的对象PageInfoCar carPageInfo new PageInfo(cars, 3);System.out.println(carPageInfo);/*PageInfo{pageNum2, pageSize3, size3, startRow4, endRow6, total17, pages6,listPage{counttrue, pageNum2, pageSize3, startRow3, endRow6, total17, pages6, reasonablefalse, pageSizeZerofalse}[Car{id6, carNum1003, brand丰田霸道, guidePrice30.00, produceTime2000-10-11, carType燃油车},Car{id7, carNum1003, brand丰田霸道, guidePrice30.00, produceTime2000-10-11, carType燃油车},Car{id8, carNum1003, brand丰田霸道, guidePrice30.00, produceTime2000-10-11, carType燃油车}],prePage1, nextPage3, isFirstPagefalse, isLastPagefalse, hasPreviousPagetrue, hasNextPagetrue,navigatePages3, navigateFirstPage1, navigateLastPage3, navigatepageNums[1, 2, 3]}*/sqlSession.close();
}