mybatis 中直接使用 collection 进行一对多关联查询,会造成 pageHelper 分页出错,数据会少很多。

简单一点的解决办法是使用子查询:

    <select id="listCustomer" resultMap="PageCustomerItemDtoResultMap">
        select
        c.id
        from `customer` c
        ORDER BY c.id desc
    </select>
    <resultMap id="PageCustomerItemDtoResultMap" type="com.zh.crm.customer.dto.CustomerItemDto">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <collection property="customerIntentions" ofType="com.zh.crm.customer.entity.CustomerIntention"
                    javaType="java.util.List" column="id" select="queryIntentionById">
        </collection>
        <collection property="labels" ofType="com.zh.crm.customer.dto.CustomerLabel"
                    javaType="java.util.List" column="id" select="queryLabelById">
        </collection>
    </resultMap>
    <select id="queryIntentionById" parameterType="java.lang.Integer" resultMap="customerIntention">
        select
        ci.type

        from customer c
        left join customer_type_label ctl on ctl.customer_id=c.id
        inner join customer_intention ci on ci.customer_id=c.id and ci.type=ctl.type_id
        where c.id=#{id}
    </select>
    <resultMap id="customerIntention" type="com.zh.crm.customer.entity.CustomerIntention">
        <result column="type" property="type"/>
    </resultMap>
    <select id="queryLabelById" parameterType="java.lang.Integer" resultMap="customerLabel">
        select
        IF(c.is_register=1, gd.name,"线索") as label,
        if(c.`is_register`,ci.grade_name,'D') as grade_name

        from customer c
        inner join customer_type_label ctl on ctl.customer_id=c.id
        left join `ge_dictionary` gd on gd.id=ctl.type_id
        left join customer_intention ci on ci.customer_id=c.id and ci.type=ctl.type_id
        where c.id=#{id}
    </select>
    <resultMap id="customerLabel" type="com.zh.crm.customer.dto.CustomerLabel">
        <result column="label" property="typeName"/>
        <result column="grade_name" property="gradeName"/>
    </resultMap>

Request:

{
  "pageInfo": {
    "count": 10,
    "offset": 0,
    "page": 1,
    "pageSize": 0,
    "startRow": 0
  },
  "searchContent": "",
  "sortType": 1
}

Response:

{
  "code": "0",
  "reason": "成功",
  "data": {
    "total": 18,
    "list": [
      {
        "id": 22,
        "labels": [
          {
            "typeName": "求租",
            "gradeName": "A"
          },
          {
            "typeName": "求购",
            "gradeName": "C"
          },
          {
            "typeName": "出租",
            "gradeName": "B"
          },
          {
            "typeName": "出售",
            "gradeName": "A"
          }
        ],
        "customerIntentions": [
          {
            "type": 200101
          },
          {
            "type": 200102
          }
        ]
      },
      {
        "id": 21,
        "labels": [
          {
            "typeName": "线索",
            "gradeName": "D"
          }
        ],
        "customerIntentions": []
      },
      {
        "id": 17,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 24,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 11,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 10,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 16,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 9,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 8,
        "labels": [],
        "customerIntentions": []
      },
      {
        "id": 7,
        "labels": [],
        "customerIntentions": []
      }
    ],
    "pageNum": 1,
    "pageSize": 10,
    "size": 10,
    "startRow": 1,
    "endRow": 10,
    "pages": 2,
    "prePage": 0,
    "nextPage": 2,
    "isFirstPage": true,
    "isLastPage": false,
    "hasPreviousPage": false,
    "hasNextPage": true,
    "navigatePages": 8,
    "navigatepageNums": [
      1,
      2
    ],
    "navigateFirstPage": 1,
    "navigateLastPage": 2
  }
}

 

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐