采用PageHelper分页助手


<!--pagehelper:通用Mapper分页-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

在这里插入图片描述

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DictionaryResult<T> {

    private  Long total;
    private List<T> rows;



}

查询功能:Controller

    @GetMapping(value = "/find")
    public ResponseEntity<DictionaryResult<Dictionary>> queryStandard(Integer page, Integer rows){
        try {
            // 查询数据
            DictionaryResult<Dictionary> result = this.dictionaryService.queryStandardByPage(page, rows);
            if(result != null){
                // 资源存在,返回200
                return new ResponseEntity<>(result, HttpStatus.OK);
            }
            // 资源不存在,返回404
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } catch (Exception e) {
            e.printStackTrace();
            // 出现异常,返回500
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

查询功能:Service

 @Override
    public DictionaryResult<Dictionary> queryStandardByPage(Integer page, Integer rows) {
        // 1.使用分页助手开始分页,指定两个参数:当前页码,每页条数
        PageHelper.startPage(page, rows);
        // 2,然后分页拦截器会自动对接下来的查询进行分页
        List<Dictionary> userDomains = dictionaryDao.findAll();
        // 3,将list封装到分页结果中
        PageInfo<Dictionary> pageInfo = new PageInfo<>(userDomains);
        // 4,将数据封装到DictionaryResult中

        DictionaryResult<Dictionary> result = new DictionaryResult(pageInfo.getTotal(), userDomains);

        return result;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值