關聯查詢時,需使用獨立子查詢才能使結果數量正確
實體類 Resident
public class Resident implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主鍵id
*/
@TableId(type = IdType.ID_WORKER_STR)
@ApiModelProperty(value = "主鍵id")
private java.lang.String id;
/**
* 姓名
*/
@Excel(name = "姓名", width = 15)
@ApiModelProperty(value = "姓名")
private java.lang.String residentName;
/**
* 所屬列表
*/
@TableField(exist = false)
private List<Hourse> hourseList;
}
實體類Hourse
public class Hourse implements Serializable {
private static final long serialVersionUID = 1L;
/**主鍵id*/
@TableId(type = IdType.ID_WORKER_STR)
@ApiModelProperty(value = "主鍵id")
private java.lang.String id;
/**門牌*/
@Excel(name = "門牌", width = 15)
@ApiModelProperty(value = "門牌")
private java.lang.String number;
}
實體類ResidentHourse
public class ResidentHourse implements Serializable {
private static final long serialVersionUID = 1L;
/**主鍵id*/
@TableId(type = IdType.ID_WORKER_STR)
@ApiModelProperty(value = "主鍵id")
private java.lang.String id;
/**住戶id*/
@Excel(name = "住戶id", width = 15)
@ApiModelProperty(value = "住戶id")
private java.lang.String hourseId;
/**居民id*/
@Excel(name = "居民id", width = 15)
@ApiModelProperty(value = "居民id")
private java.lang.String residentId;
/**居住類型:1、業主 2、租住*/
@Excel(name = "居住類型:1、業主 2、租住", width = 15)
@ApiModelProperty(value = "居住類型:1、業主 2、租住")
private java.lang.String liveType;
/**成員類型:1、戶主 2、成員*/
@Excel(name = "成員類型:1、戶主 2、成員", width = 15)
@ApiModelProperty(value = "成員類型:1、戶主 2、成員")
private java.lang.String memberType;
@TableField(exist = false)
private Hourse hourse;
}
xml
<resultMap id="hourseMap" type="org.entity.Hourse">
<id property="id" column="hourse_id"></id>
<result property="number" column="number"></result>
</resultMap>
<resultMap id="residentHourseMap" type="org.entity.ResidentHourse">
<id property="id" column="id"></id>
<result property="liveType" column="live_type"></result>
<result property="memberType" column="member_type"></result>
<association property="hourse" resultMap="hourseMap"></association><!--一對一-->
</resultMap>
<!--帶列表,-->
<resultMap id="residentMap" type="org.entity.Resident">
<id property="id" column="id"></id>
<result property="residentName" column="resident_name"></result>
<collection property="hourseList" column="id" select="selectHourseList"></collection>
</resultMap>
<!--sql 片段 查詢條件部分-->
<sql id="srhopt1">
。。。
</sql>
<sql id="srhopt2">
。。。
</sql>
<!--主查詢-->
<select id="queryList" resultMap="residentMap">
select r.* from hos_resident r
where EXISTS(
select * from hos_resident_hourse rh
left join v_hourse vh on vh.hourse_id=rh.hourse_id
where vh.company_id=#{params.companyId}
and rh.resident_id=r.id
<include refid="srhopt1"></include>
)
<include refid="srhopt2"></include>
order by r.py
</select>
<!--子查詢-->
<select id="selectHourseList" resultMap="residentHourseMap">
select * from hos_resident_hourse rh
left join v_hourse vh on vh.hourse_id=rh.hourse_id
where rh.resident_id=#{id}
</select>
Mapper接口
IPage<Resident> queryList(Page page, @Param("params") Resident resident);
重點是這裏,使用獨立主查詢再查子列表,避免查詢總數不正確,並且子列表也沒有正確添加到主記錄中
<collection property="hourseList" column="id" select="selectHourseList"></collection>