前言
分页查询是在web开发中常用的一种技术,当某个页面查询返回的数据量较大时,为了提高性能和用户体验不能将所有数据一次性返回给过前端,这时候就需要用到分页查询了
PageHelper是一款开源的Mybatis第三方物理分页插件,spring boot项目中集成PageHelper插件非常简单,下面将为大家详细介绍;
插件地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
1.引入依赖
在上一篇文章Mybatis 实现基本的增删改查 (基于Mybatis-generator插件方式)的基础上,在pom.xml中添加如下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2.Mapper中接口
在EmployeeMapper.java中新增findByPaging接口,接口返回类型为Page
public interface EmployeeMapper {
int deleteByPrimaryKey(Long id);
int insert(Employee record);
int insertSelective(Employee record);
Employee selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record);
Page<Employee> findByPaging(Map param);
}
3.修改XML文件
在EmployeeMapper.xml中添加上面接口对应Sql查询语句,可以看到使用插件的方式查询时,这里入参的类型为com.github.pagehelper.Page
<select id="findByPaging" resultMap