自学内容网 自学内容网

MyBatis踩坑记录-多表关联字段相同,字段数据覆盖问题

1. 背景描述

现有一下业务,单个任务下可能会有多个子任务,现将通过关联查询将子任务放在单个任务下,类似于这样的数据结构:

{
    "task": {
        "id": "1",
        "name": "a",
        "createTime": "2024-07-03 17:15:00"
    },
    "info": [
        {
            "id": "01",
            "name": "aa",
            "createTime": "2024-07-03 17:15:00"
        },
        {
            "id": "001",
            "name": "aaa",
            "createTime": "2024-07-03 17:15:00"
        },
        {
            "id": "0001",
            "name": "aaaa",
            "createTime": "2024-07-03 17:15:00"
        }
    ]
}

2. 实体记录

public class TaskInfoVO {
    private TaskDO task;
    private List<SubTaskInfo> subTaskInfos;
}

/**
 * 任务总表
 * @TableName task_list
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TaskDO implements Serializable {
    private Long id;
    private String name;
    private Date createTime;
    private static final long serialVersionUID = 1L;
}

/**
 * 子任务
 * @TableName task_list
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.ALWAYS)
public class SubTaskInfo implements Serializable {
private Long id = 0L;
private String name;
    private Date createTime;
    private static final long serialVersionUID = 1L;

3. 错误映射


<resultMap id="TaskInfoVOMap" type="com.iqiyi.auto.module.heal.controller.VO.TaskInfoVO">
        <association property="task" javaType="com.iqiyi.auto.module.heal.DO.TaskDO">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        </association>
        <collection property="subTaskInfos" ofType="com.iqiyi.auto.module.heal.DTO.SubTaskInfo" notNullColumn="id">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        </collection>
    </resultMap>

3.1 造成的影响

错误图片

4. 解决办法

4.1 修改映射文件


<resultMap id="TaskInfoVOMap" type="com.iqiyi.auto.module.heal.controller.VO.TaskInfoVO">
        <association property="task" javaType="com.iqiyi.auto.module.heal.DO.TaskDO">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="name" column="task_name" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        </association>
        <collection property="subTaskInfos" ofType="com.iqiyi.auto.module.heal.DTO.SubTaskInfo" notNullColumn="id">
            <id property="id" column="id_sub" jdbcType="BIGINT"/>
            <result property="name" column="name_sub" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time_sub" jdbcType="TIMESTAMP"/>
        </collection>
    </resultMap>

5. 修复后的效果

5.1 返回的数据

修复后的截图

5.2 正确展示

正确展示

7. end ~

一天上一当,当当不一样,坑坑多多


原文地址:https://blog.csdn.net/qq_38869359/article/details/140157507

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!