關聯查詢時,需使用獨立子查詢才能使結果數量正確
實體類 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;
}
xml
<resultMap id="hourseMap" type="org.entity.Hourse">
<id property="id" column="hourse_id"></id>
<result property="number" column="number"></result>
</resultMap>
<!--帶列表,-->
<resultMap id="residentMap" type="org.jeecg.modules.hoscard.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="hourseMap">
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>