1.导入jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelperboot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
2.启动类添加配置文件
// 解决springboot 与pageHelper分页查询问题
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
3.测试类
@PostMapping("listpage")
public @ResponseBody
ResponsePageData<CipherEntity> listpage(
@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String appName, String type
) {
ResponsePageData<CipherEntity> ret = new ResponsePageData<>();
ret.setCode("200");
CipherEntity entity = new CipherEntity();
entity.setAppName(appName);
entity.setType(type);
Page<CipherEntity> data = PageHelper.startPage(page, limit);
List<CipherEntity> acts = iCipherService.listpage(null, entity);
if (acts.size() == 0) {
ret.setMessage("没有查询到数据");
} else {
ret.setTotalCount((int) data.getTotal());
ret.setList(acts);
ret.setMessage("SUCCESS");
}
return ret;
}

本文介绍了如何在SpringBoot项目中整合PageHelper库进行分页查询。首先通过引入PageHelper的starter和核心jar包,并排除特定依赖。然后在启动类中配置PageHelper,设置相关属性如offsetAsPageNum、rowBoundsWithCount等。最后在测试类中展示如何使用PageHelper进行分页查询,返回包含总条数和数据列表的响应。

2317

被折叠的 条评论
为什么被折叠?



