mybatis一对多表查询返回记录不正常问题解决方案

一、现在有两个表:

1)主表:t_party_building_notice,为党建的通知表。

党建通知实体类:TPartyBuildingActivety.java

@Data
public class TPartyBuildingActivity extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** ID */
    private String id;

    /** 项目ID */
    @Excel(name = "项目ID")
    private Long siteId;

    /** 所属公司 */
    @Excel(name = "所属公司")
    private String buildingCorp;

    /** 党建活动分类 */
    @Excel(name = "党建活动分类")
    private Long activityType;

    /** 党建活动标题 */
    @Excel(name = "党建活动标题")
    private String activityName;

    /** 党建活动内容 */
    @Excel(name = "党建活动内容")
    private String activityContent;

    /** 审核人 */
    @Excel(name = "审核人")
    private String operator;

    @Excel(name = "图片")
    private List<TPhotos> photos;

}

2) 副表:t_photos,为党建消息中的图片管理,

图片管理实体类:TPhotos.java

@Data
public class TPhotos extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 每张图片的唯一编号,不重复 */
    private int id;

    /** 对应应用记录的id,例如与 党建公告中的 id */
    @Excel(name = "对应应用记录的id")
    private String photoId;

    /** 图片地址 */
    @Excel(name = "图片地址")
    private String photoUrl;

    /** 图片显示顺序 */
    @Excel(name = "图片显示顺序")
    private Integer photoShowOrder;

}

二、功能要求,查询党建通知时,检索出每个党建记录所关联的图片列表,并且以数组的方式返回。如便前端更好的显示图片。

1)mybaits XML的代码如下:

<resultMap type="TPartyBuildingActivity" id="TPartyBuildingActivityResult">
        <result property="id"    column="id"    />
        <result property="siteId"    column="site_id"    />
        <result property="buildingCorp"    column="building_corp"    />
        <result property="activityType"    column="activity_type"    />
        <result property="activityName"    column="activity_name"    />
        <result property="activityContent"    column="activity_content"    />
        <result property="operator"    column="operator"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <collection property="photos"  ofType="com.ruoyi.partybuilding.domain.TPhotos" resultMap="TPhotosResult"></collection>
    </resultMap>

    <resultMap type="TPhotos" id="TPhotosResult">
        <result property="photoId"    column="photo_id"    />
        <result property="photoUrl"    column="photo_url"    />
        <result property="photoShowOrder"    column="photo_show_order"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

2)mybatis XML中的方法如下:

<sql id="selectTPartyBuildingActivityVo">
        select a.id, a.site_id, a.building_corp, a.activity_type, a.activity_name, a.activity_content, a.operator,
        a.create_time, a.update_time, b.photo_url, b.photo_show_order, b.photo_id
        from t_party_building_activity a
        left join t_photos b on a.id = b.photo_id
    </sql>

    <select id="selectTPartyBuildingActivityList" parameterType="TPartyBuildingActivity" resultMap="TPartyBuildingActivityResult">
        <include refid="selectTPartyBuildingActivityVo"/>        
        order by a.create_time desc
    </select>

三、查询结果,如果 t_photos表中至少有一条记录时,返回结果正常,如果 t_photos没有包含 t_party_building_notice党建公告图片时,返回一条空的 t_photos记录,这是条不存在的记录。图片如下:

四、以上结果肯定不是我想要的,经排查,问题出在 XML中 返回的TPhotosResult中。

 

将上面TPhotsResult中的 <result property="createTime" column= "create_time"/><result property="updateTime" column = "update_time"/>删除,重新运行,如果 t_photos中没有记录时,将返回 photos: [],结果完全正确。

<resultMap type="TPhotos" id="TPhotosResult">
        <result property="photoId"    column="photo_id"    />
        <result property="photoUrl"    column="photo_url"    />
        <result property="photoShowOrder"    column="photo_show_order"    />
    </resultMap>

 正确的结果:

五、问题分析,因为副表中的字段与主表字段相同,导致此类问题的发生。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值