1、pom.xml安装依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
2、配置
application.yml
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
resource/mybatis-config.xml
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
3、启动类
@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)
排除自动化配置,否则运行时会提示:在系统中发现了多个分页插件,请检查系统配置!
4、使用
controller
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
@PostMapping("/findListByPage")
public BaseResponse findListByPage(@Validated @RequestBody BasePage req, BindingResult results){
Page listParam = new Page(req.getPageNum(), req.getPageSize());
PageInfo<User> pageInfo = userService.findListByPage(listParam);
return BaseResponse.success(pageInfo);
}
获取分页数据
@Override
public PageInfo<User> findListByPage(Page listParam) {
// 两种方法
// 方法一:
// 注意引入顺序,startPage调用完之后需要立即调用sql,否则会导致分页无法结束,下次调用sql拼接limit
// 设置分页参数
PageHelper.startPage(listParam.getPageNum(),listParam.getPageSize(), true);
// 进行分页查询
List<User> userList = userMapper.findListByPage();
// 获取分页信息
PageInfo<User> pageInfo = new PageInfo<>(userList);
return pageInfo;
// 方法二:
return PageHelper.startPage(listParam.getPageNum(), listParam.getPageSize()).doSelectPageInfo(() -> userDao.findListByPage());
}
出现问题:
User实体继承了Page类,导致反序列化一直失败,警醒!!!