1.两个实体类

djTag.java

/** 其他实体 */
private * *;    

/** 标签设备关联信息 */   
 private List<DjTagDevice> djTagDeviceList;

/** 其他实体 */
private * *;

djTagDevice.java

    /** 主键 */
    private Long id;

    /** 设备ID */
    @Excel(name = "设备ID")
    private Long deviceId;

    /** 标签ID */
    @Excel(name = "标签ID")
    private Long tagId;

在一个实体类中加入存放嵌套的数据集合List<DjTagDevice>类型的 djTagDeviceList;

2.在djTagMapper.xml文件中加入

    <resultMap type="DjTag" id="DjTagResult">
        <result property="id"    column="id"    />
        <result property="tagName"    column="tag_name"    />
        <result property="tagType"    column="tag_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createName"    column="create_name"    />
        <result property="updateName"    column="update_name"    />
    </resultMap>

    <resultMap id="DjTagDjTagDeviceResult" type="DjTag" extends="DjTagResult">
        <collection property="djTagDeviceList" notNullColumn="sub_id" javaType="java.util.List" resultMap="DjTagDeviceResult" />
    </resultMap>

    <resultMap type="DjTagDevice" id="DjTagDeviceResult">
        <result property="id"    column="sub_id"    />
        <result property="deviceId"    column="sub_device_id"    />
        <result property="tagId"    column="sub_tag_id"    />
    </resultMap>

嵌套的数据中

<resultMap type="DjTagDevice" id="DjTagDeviceResult">
        <result property="id"    column="sub_id"    />
        <result property="deviceId"    column="sub_device_id"    />
        <result property="tagId"    column="sub_tag_id"    />
    </resultMap>

id="DjTagDeviceResult"对应<collection />中的resultMap = "DjTagDeviceResult"

3.在需要嵌套查询的sql 中,resultMap返回DjTagDjTagDeviceResult,如:

    <select id="selectDjTagList" parameterType="DjTag" resultMap="DjTagDjTagDeviceResult">
        SELECT
        a.id,
        a.tag_name,
        a.tag_type,
        a.create_time,
        a.update_time,
        a.create_name,
        a.update_name,
        a.remark,
        b.id AS sub_id,
        b.device_id AS sub_device_id,
        b.tag_id AS sub_tag_id
        FROM
        dj_tag a
        LEFT JOIN dj_tag_device b ON b.tag_id = a.id
        where ****
        GROUP BY a.tag_name
    </select>

这样嵌套查询就写好了!!!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐