案例地址: https://gitee.com/vinci99/paging-pagehelper-demo/tree/master
1、集成pagehelper
<!-- 集成pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
2、配置pagehelper
這里使用application.properties類型配置文件來(lái)做例子
#配置pagehelper
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
3、編寫(xiě)代碼
創(chuàng)建一個(gè)持久化對(duì)象TestUserPO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestUserPO {
/**
* @description: 編號(hào)
**/
private Integer id;
/**
* @description: 姓名
**/
private String name;
}
編寫(xiě)在Mapper中編寫(xiě)SQL語(yǔ)句查詢與之對(duì)應(yīng)的表
<!-- SQL這里不用寫(xiě)分頁(yè) -->
<select id="getInfo" resultType="com.vinci.demo.entity.TestUserPO">
SELECT
id,
name
FROM test_user
</select>
在業(yè)務(wù)層調(diào)用Mapper接口獲取數(shù)據(jù)并分頁(yè);需要注意:PageHelper.startPage(pageNum,pageSize)必須寫(xiě)在查詢前面,否則將不會(huì)生效
/**
* @description: mapper接口
* @author: Vinci
* @date: 2023/9/2 12:23
**/
@Resource
private TestUserMapper testUserMapper;
/**
* @description: 測(cè)試分頁(yè)
* @author: Vinci
* @date: 2023/9/2 12:18
**/
@Override
public PageInfo<TestUserPO> getInfo(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<TestUserPO> info = testUserMapper.getInfo();
return new PageInfo<>(info);
}
繼續(xù)編寫(xiě)Controller層代碼,將數(shù)據(jù)返回給前端文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-690963.html
/**
* @description: 日志服務(wù)
**/
private static final Logger log = LoggerFactory.getLogger(TestController.class);
/**
* @description: 業(yè)務(wù)層接口
**/
@Resource
private TestUserService testUserService;
/**
* @description: 測(cè)試分頁(yè)
* @author: Vinci
* @param pageNum 頁(yè)碼
* @param pageSize 頁(yè)長(zhǎng) (每頁(yè)多少條數(shù)據(jù))
* @date: 2023/9/2 12:18
**/
@GetMapping("/getInfo")
public Object getInfo(
@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
@RequestParam(value = "pageSize",defaultValue = "15")Integer pageSize
){
try{
return testUserService.getInfo(pageNum,pageSize);
}catch (Exception e){
log.error("發(fā)現(xiàn)異常,",e);
return e.getMessage();
}
}
4、分頁(yè)效果
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-690963.html
到了這里,關(guān)于SpringBoot 使用MyBatis分頁(yè)插件實(shí)現(xiàn)分頁(yè)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!