一、MyBatis分页插件
1、有关分页功能的计算
SQL语句分页:limit index,pageSize
pageSize:每页显示的条数
pageNum:当前页的页码
index:当前页的起始索引,index=(pageNum - 1) * pageSize;
count:总记录数
totalPage:总记录数
totalPage:总页数
totalPage = count / pageSize;
if(count % pageSize !=0){
totalPage += 1;
}
pageSize=4,pageNum=1,index=0 limit 0,4
pageSize=4,pageNum=2,index=8 limit 8,4
pageSize=4,pageNum=6,index=20 limit 20,4
2、使用步骤
2.1、添加依赖pom.xml
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
2.2、配置分页插件mybatis-config.xml
<plugins>
<!--配置分页插件-->
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
2.3、测试类
public class PageTest {
@Test
public void Test(){
SqlSession sqSession = SqlSessionUtil.getSqSession();
EmpMapper mapper = sqSession.getMapper(EmpMapper.class);
// 查询功能之前,开启分页功能
Page<Object> objects = PageHelper.startPage(1, 4);
List<Emp> list = mapper.selectByExample(null);
// 查询功能之后可以获取分页相关的所有数据
// PageInfo<Emp> pageInfo = new PageInfo<>(list,5);
list.forEach(System.out::println);
System.out.println(objects);
}
}