mybatis pagehelper 分页

引入依赖

    <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>4.1.0</version>
            </dependency>

mybatisConfig配置

复制代码
@Configuration
public class MyBatisConfig {
     @Bean
        public PageHelper pageHelper(){
            PageHelper pageHelper = new PageHelper();
            Properties p = new Properties();
            p.setProperty("offsetAsPageNum","true");
            p.setProperty("rowBoundsWithCount","true");
            p.setProperty("reasonable","true");
            pageHelper.setProperties(p);
            return pageHelper;
        }
}
复制代码
复制代码
mapper

@Select("select * from video") List<Video> findAllVideo();


Controller

@RequestMapping(value = "/lists") @ResponseBody @CrossOrigin//允许浏览器跨域 因为电脑垃圾 eclipse 和postman同时使用太卡 所以用的一个在线测试网站,网址放下面 public Object lists(HttpServletRequest request){ PageHelper.startPage(0, 1); return test.findAllVideo(); }
复制代码

网址:https://getman.cn/

效果:

复制代码
[
    {
        "id": 1,
        "title": "SpringBoot+Maven整合Websocket课程",
        "summary": "这是概要",
        "cover_img": null,
        "view_num": null,
        "price": 1000,
        "create_time": null,
        "online": 0,
        "point": 8
    }
]
复制代码

使用PageInfo

复制代码
 @RequestMapping(value = "/lists")
     @ResponseBody
     @CrossOrigin
     public Object lists(HttpServletRequest request){
         
         PageHelper.startPage(0, 1);
         List<Video>  list=test.findAllStudent();
         PageInfo<Video> pageInfo = new PageInfo<>(list);
          return pageInfo;
                 
     }
     
复制代码

返回参数如下:

复制代码
{
    "pageNum": 1,
    "pageSize": 1,
    "size": 1,
    "orderBy": null,
    "startRow": 1,
    "endRow": 1,
    "total": 9,
    "pages": 9,
    "list": [
        {
            "id": 1,
            "title": "SpringBoot+Maven整合Websocket课程",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 1000,
            "create_time": null,
            "online": 0,
            "point": 8
        }
    ],
    "firstPage": 1,
    "prePage": 0,
    "nextPage": 2,
    "lastPage": 8,
    "isFirstPage": true,
    "isLastPage": false,
    "hasPreviousPage": false,
    "hasNextPage": true,
    "navigatePages": 8,
    "navigatepageNums": [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8
    ]
}
复制代码

 封装数据

复制代码
 @GetMapping(value = "/lists")
     @ResponseBody
     @CrossOrigin
     public Object lists(@RequestParam(value="page",defaultValue="1")int page,
              @RequestParam(value="size",defaultValue="10")int size){
         
         PageHelper.startPage(page, size);
         List<Video>  list=test.findAllStudent();
         PageInfo<Video> pageInfo = new PageInfo<>(list);
         Map<String,Object> data=new HashMap<>();
         //总条数
         data.put("total_size", pageInfo.getTotal());
         //总页数
         data.put("total_page", pageInfo.getPages());
         //当前页
         data.put("current_page", page);
         //数据
         data.put("data", pageInfo.getList());
         return data;
          //return test.findAllStudent();
                 
     }
     
复制代码

返回数据

复制代码
{
    "data": [
        {
            "id": 1,
            "title": "SpringBoot+Maven整合Websocket课程",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 1000,
            "create_time": null,
            "online": 0,
            "point": 8
        },
        {
            "id": 2,
            "title": "2018年 6.2新版本ELK ElasticSearch ",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 500,
            "create_time": null,
            "online": 0,
            "point": 9
        },
        {
            "id": 3,
            "title": "JMeter接口测试入门到实战",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 123,
            "create_time": null,
            "online": 0,
            "point": 8
        },
        {
            "id": 4,
            "title": "Spring Boot2.x零基础入门到高级实战",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 199,
            "create_time": null,
            "online": 0,
            "point": 6
        },
        {
            "id": 5,
            "title": "亿级流量处理搜索",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 10,
            "create_time": null,
            "online": 0,
            "point": 9
        },
        {
            "id": 6,
            "title": "reidis消息队列高级实战",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 10,
            "create_time": null,
            "online": 0,
            "point": 6
        },
        {
            "id": 7,
            "title": "谷歌面试题",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 23,
            "create_time": null,
            "online": 0,
            "point": 5
        },
        {
            "id": 8,
            "title": "js高级前端视频",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 442,
            "create_time": null,
            "online": 0,
            "point": 8
        },
        {
            "id": 9,
            "title": "List消息队列高级实战",
            "summary": "这是概要",
            "cover_img": null,
            "view_num": null,
            "price": 32,
            "create_time": null,
            "online": 0,
            "point": 4
        }
    ],
    "total_size": 9,
    "total_page": 1,
    "current_page": 1
}
复制代码

 

本文作者:KwFruit

本文链接:https://www.cnblogs.com/mangoubiubiu/p/12906777.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   KwFruit  阅读(291)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起