Mybatis 返回结果多重嵌套List

实际开发中,在定义接口的返回格式时往往会遇到返回json多重嵌套的情况:

  • 我们可以通过resultMap自动映射来实现
    <resultMap type="PersonDetailsVo" id="personDetailsMap">
        <result column="sequence_id" property="sequenceId" />
        <collection property="personPosGroup" javaType="java.util.List" ofType="com.ruoyi.project.business.main.domain.PersonPosGroup">
            <result column="camera_id" property="cameraId" />
            <collection property="personData" javaType="java.util.List" ofType="com.ruoyi.project.business.main.domain.PersonData">
                <result column="person_id" property="personId" />
                <result column="position_info" property="positionInfo" />
                <result column="worker_type" property="workerType" />
            </collection >
        </collection >
    </resultMap>

比如这样的resultMap结构就实现了三层嵌套,我们要注意创建PersonPosGroup、PersonData实体类来完成映射。

  • PersonPosGroup
public class PersonPosGroup implements Serializable {

    private String cameraId;
    private List<PersonData> personData;

    public String getCameraId() {
        return cameraId;
    }

    public void setCameraId(String cameraId) {
        this.cameraId = cameraId;
    }

    public List<PersonData> getPersonData() {
        return personData;
    }

    public void setPersonData(List<PersonData> personData) {
        this.personData = personData;
    }
}
  • PersonData
public class PersonData implements Serializable {

    private String personId;
    private String workerType;
    private JSONObject positionInfo;

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

    public String getWorkerType() {
        return workerType;
    }

    public void setWorkerType(String workerType) {
        this.workerType = workerType;
    }

    public JSONObject getPositionInfo() {
        return positionInfo;
    }

    public void setPositionInfo(String positionInfo) {
        this.positionInfo = JSONObject.parseObject(positionInfo);
    }
}

  • sql代码
<select id="selectPersonInfoBySequenceId" parameterType="java.util.Map" resultMap="personDetailsMap">
        select
            sequence_id,
            camera_id,
            person_id,
            position_info,
            worker_type
        from video_person_details_info
        where
            1=1
            <if test="video_id != null  and video_id != ''"> and video_id = #{video_id}</if>
            <if test="start_sequence_id != null">
              and sequence_id >= #{start_sequence_id} and  #{end_sequence_id} > sequence_id
            </if>
    </select>

查询数据会根据层级关系自动映射出查询结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值