springboot 集成 Mybatis pagehelper

315 阅读1分钟
  1. 引入pom 插件依赖

<mybatis.spring.version>2.2.0</mybatis.spring.version>
<mybatis.pagehelper.version>1.3.1</mybatis.pagehelper.version>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis.spring.version}</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>${mybatis.pagehelper.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  1. 配置文件配置(yml)
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
  1. 代码使用
@Data
public class PolicyListReqDTO implements Serializable {

    private PageInfo<Void> pageInfo;

    private String customerNo;
}


@GetMapping("/v1/policies")
public Response<PageInfo<Policy>> getPolicyListV1(@RequestBody PolicyListReqDTO policyListReq) {

    PageHelper.startPage(policyListReq.getPageInfo().getPageNum(), policyListReq.getPageInfo().getPageSize());
    List<Policy> policyList = policyService.getPolicyList(policyListReq.getCustomerNo());
    return RespUtils.success(new PageInfo<>(policyList));
}

  1. 测试

请求

GET http://localhost:8071/life-policy/outer/v1/policies
Content-Type: application/json

{
  "pageInfo": {
    "pageNum": 2,
    "pageSize": 6
  },
  "customerNo": "C100086"
}

响应


GET http://localhost:8071/life-policy/outer/v1/policies

HTTP/1.1 200 OK
Date: Fri, 22 Oct 2021 15:01:28 GMT
Content-Type: application/json
Transfer-Encoding: chunked

{
  "code": "00",
  "msg": "Success",
  "data": {
    "total": 15,
    "list": [
      {
        "policyNo": "P10000000000007",
        "policyStatus": 10
      },
      {
        "policyNo": "P10000000000008",
        "policyStatus": 10
      },
      {
        "policyNo": "P10000000000009",
        "policyStatus": 10
      },
      {
        "policyNo": "P10000000000010",
        "policyStatus": 10
      },
      {
        "policyNo": "P10000000000011",
        "policyStatus": 10
      },
      {
        "policyNo": "P10000000000012",
        "policyStatus": 10
      }
    ],
    "pageNum": 2,
    "pageSize": 6,
    "size": 6,
    "startRow": 7,
    "endRow": 12,
    "pages": 3,
    "prePage": 1,
    "nextPage": 3,
    "isFirstPage": false,
    "isLastPage": false,
    "hasPreviousPage": true,
    "hasNextPage": true,
    "navigatePages": 8,
    "navigatepageNums": [
      1,
      2,
      3
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 3
  }
}

Response code: 200 (OK); Time: 1150ms; Content length: 2937 bytes