SpringBoot使用pageHelper

pom文件增加依赖(只service需要)

  
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        1.3.1
    
    
    
        tk.mybatis
        mapper-spring-boot-starter
        1.2.4
    
    
    
        com.github.pagehelper
        pagehelper-spring-boot-starter
        1.2.3
        
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
            
        
    
**注意解决依赖冲突**

application.proprieties

pagehelper.helperDialect=mysql
pagehelper.reasonable=true  #为了使用输入页数为负或者超出最大页时候使页数为最小或最大值
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

代码范例

service
   public PageInfo findAllUser(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    List users = userMapper.selectAllUser();
    List userDtos = new ArrayList<>();

    users.forEach(user -> {
        userDtos.add(dozerBeanMapper.map(user, UserDto.class));
    });
    // 用PageInfo对结果进行包装
    PageInfo pageInfo = new PageInfo(users);
    pageInfo.setList(userDtos);
    return pageInfo;
}

controller
@ResponseBody
@RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
public PageInfo findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {

    logger.info(String.valueOf(userService.findAllUser(pageNum, pageSize)));
    return userService.findAllUser(pageNum, pageSize);
}

请求及返回结果
    {
  "pageNum": 0,
  "pageSize": 5,
  "size": 5,
  "startRow": 1,
  "endRow": 5,
  "total": 14,
  "pages": 3,
  "list": [
    {
      "userId": 1000,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1001,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1002,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1003,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    },
    {
      "userId": 1004,
      "userName": "姓名",
      "password": "123456",
      "phone": "18123568978"
    }
  ],
  "prePage": 0,
  "nextPage": 1,
  "isFirstPage": false,
  "isLastPage": false,
  "hasPreviousPage": false,
  "hasNextPage": true,
  "navigatePages": 8,
  "navigatepageNums": [
    1,
    2,
    3
  ],
  "navigateFirstPage": 1,
  "navigateLastPage": 3,
  "firstPage": 1,
  "lastPage": 3
}

你可能感兴趣的:(SpringBoot使用pageHelper)