对象嵌套list子对象,一个SQL解决

对象嵌套list返回,一般需要写两个SQL重复循环查询,看看只需要一个SQL的写法吧!

1、Resp

{
  "code": "20000",
  "data": {
    "A": "YOUR",
    "B": "SO",
    "C": "BEAUTIFUL",
    "D": "I",
    "list": [
      {
        "E": "haha",
        "F": "good job"
      },
      {
        "E": "bill",
        "F": "you are right"
      }
    ]
  },
  "msg": "SUCCESS",
  "interfaceId": "e50fadjk893jfdie9098764f5f9214a7b8"
}

2、VO

package com.goodjob.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;

@Data
@ApiModel("实例展示")
public class XxxTableVO {

    @ApiModelProperty(value = "A")
    private String A;

    @ApiModelProperty(value = "B")
    private String B;

    @ApiModelProperty(value = "C")
    private String C;

    @ApiModelProperty(value = "D")
    private String D;

    @ApiModelProperty(value = "list")
    private List<DetailsVO> list;


    @Data
    @ApiModel("DetailsVO")
    public static class DetailsVO {

        @ApiModelProperty(value = "E")
        private String E;

        @ApiModelProperty(value = "F")
        private String F;
    }
}



3、SQL

<select id="executeDetails" resultMap="executeDetailsMap">
        SELECT
              t1.a AS A,
              t1.b AS B,
              t1.c AS C,
              t1.d AS D,
              t2.e AS E,
              t2.f AS F
         FROM xxxTable AS t1
        INNER JOIN xxxDetails AS t2 ON t2.id = t1.details_id
        WHERE t2.id = #{Id}
        ORDER BY t2.id DESC
    </select>
    <resultMap id="executeDetailsMap" type="com.goodjob.vo.XxxTableVO">
        <result column="A" property="A"/>
        <result column="B" property="B"/>
        <result column="C" property="C"/>
        <result column="D" property="D"/>
        <collection property="list" javaType="java.util.ArrayList"
                    ofType="com.goodjob.vo.XxxTableVO$DetailsVO">
                    // 注意的一点 如果子集里数据重复他会合并
            <result column="E" property="E"/>
            <result column="F" property="F"/>
        </collection>
    </resultMap>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值