查看admin列表 分页显示

根据前端获取的数据封装一个JavaBean

public class PageGridResult {
    // 当前页数
    private Long page;
    // 总页数
    private Long total;
    // 总记录数
    private long records;
    // 每行显示的内容
    private List<?> rows;


    public Long getPage() {
        return page;
    }

    public void setPage(Long page) {
        this.page = page;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public long getRecords() {
        return records;
    }

    public void setRecords(long records) {
        this.records = records;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }
}

首先根据文档配置分页插件: https://baomidou.com/guide/interceptor.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F-%E4%BB%A5%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6%E4%B8%BE%E4%BE%8B

@Configuration
public class MybatisPlusConfig {

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

CURD文档: https://baomidou.com/guide/crud-interface.html#select
新建一个Page对象 此对象用于封装分页条件如: 第几页 每页显示多少条

 Page<AdminUser> adminUserPage = new Page<>(page, pageSize);

调用selectPage分页查询 查询结果将反射至Page对象中

        adminMapper.selectPage(adminUserPage, queryWrapper);
    @Override
    public IPage<AdminUser> queryUserPage(Integer page, Integer pageSize) {
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderBy(true, true, "created_time");

        Page<AdminUser> adminUserPage = new Page<>(page, pageSize);

        adminMapper.selectPage(adminUserPage, queryWrapper);


        return adminUserPage;
    }

controller就很简单了根据前端传入的分页条件调用service类 并创建之前封装的javaBean对象 将数据赋值返回即可
若前端未传入数据 则设置一个默认值查询即可

    @Override
    public GraceJSONResult getAdminList(Integer page, Integer pageSize) {
        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 10;
        }

        IPage<AdminUser> adminUserIPage = adminService.queryUserPage(page, pageSize);

        PageGridResult pageGridResult = new PageGridResult();

        pageGridResult.setPage(adminUserIPage.getPages());
        pageGridResult.setRecords(adminUserIPage.getRecords().size());
        pageGridResult.setTotal(adminUserIPage.getTotal());
        pageGridResult.setRows(adminUserIPage.getRecords());

        return GraceJSONResult.ok(pageGridResult);
    }
posted @   RainbowMagic  阅读(92)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示