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
}

 

posted @ 2020-05-17 20:43  KwFruit  阅读(288)  评论(1编辑  收藏  举报