一、现在有两个表:
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>
正确的结果:
五、问题分析,因为副表中的字段与主表字段相同,导致此类问题的发生。