关联查询时,需使用独立子查询才能使结果数量正确
实体类 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>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐