java mybatis plus 树结构、联表查询、查询语句相互调用

实体VO,即返回的数据实体

@ApiModel(" 区域 间隔 表")
public class PlaceVo implements Serializable {
   
    @ApiModelProperty(value = "子类区域间隔", dataType = "List")
    List<PlaceVo> subList;

    @ApiModelProperty(value = "被测设备表")
    List<PrimaryDeviceVo> primaryDeviceList;

    private Long placeId;
 
    @ApiModelProperty(value = "名称")
    private String placeName;
}


@Data
public class PrimaryDeviceVo extends PrimaryDevice {
    @ApiModelProperty(value = "被测设备部件表")
    List<PartsVo> partsList;

    @ApiModelProperty(value = "被测设备id")
    private Long id;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "编号")
    private String code;
}

@Data
@ApiModel(" 区域 间隔 表")
public class PartsVo implements Serializable {

    @ApiModelProperty(value = "部位id")
    private Long id;

    @ApiModelProperty(value = "名称")
    private String name;

}




    public List<PlaceVo> listTreeAll() {
//        List<PrimaryDeviceVo> primaryDeviceVos = primaryDeviceMapper.listTree(1L);
        return baseMapper.listTreeAll(0L);
    }

List<PlaceVo> listTreeAll(Long parentId);


<mapper namespace="com.into.smart.ele.manager.mapper.PlaceMapper">

    <resultMap id="deviceTreeEntity" type="com.into.smart.ele.manager.entity.vo.PlaceVo">
        <id property="placeId" column="place_id"/>
        <id property="placeName" column="place_name"/>
        <id property="parentId" column="parent_id"/>
        <!--collection:一对多
            assocication:一对一
            -->

        <collection property="subList" column="place_id"
                    ofType="com.into.smart.ele.manager.entity.vo.PlaceVo"
                    select="com.into.smart.ele.manager.mapper.PlaceMapper.listTreeAll">
        </collection>
        <collection property="primaryDeviceList" column="place_id"
                    ofType="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo"
                    select="com.into.smart.ele.manager.mapper.PrimaryDeviceMapper.listTree">
        </collection>
    </resultMap>

    <select id="listTreeAll" resultMap="deviceTreeEntity">
        SELECT epi.place_id,epi.place_name,epi.parent_id
        from ele_place_info epi
        <where>
            <if test="parentId != null">parent_id=#{parentId}</if>
        </where>
    </select>
</mapper>


被调用的另一个mapper

@Mapper
public interface PrimaryDeviceMapper extends BaseMapper<PrimaryDevice> {

    List<PrimaryDeviceVo> getList(@Param("params") PrimaryDeviceDto params);

    /**
     * 设备部位树
     *
     * @return
     */
    List<PrimaryDeviceVo> listTree(Long placeId);
}



<mapper namespace="com.into.smart.ele.manager.mapper.PrimaryDeviceMapper">
    <resultMap id="deviceTreeEntity" type="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo">
        <!--        <id property="factory.name" column="factory.name"/>-->
        <!--        <id property="site.siteName" column="site.siteName"/>-->
        <!--        <result property="alarmEvents" column="alarm_events"/>-->
        <!--collection:一对多
            assocication:一对一
            -->

        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <collection property="partsList" ofType="com.into.smart.ele.manager.entity.vo.PartsVo">
            <result property="id" column="c_t_id"/>
            <result property="name" column="c_t_name"/>
        </collection>

    </resultMap>

    <select id="getList" resultType="com.into.smart.ele.manager.entity.vo.PrimaryDeviceVo">
        SELECT
        pinfo.place_name,
        jinfo.place_name AS jgName,
        dtype.type_level,
        dtype.type_name,
        dinfo.*
        FROM
        ele_place_info pinfo
        LEFT JOIN ele_place_info jinfo ON pinfo.place_id = jinfo.parent_id
        INNER JOIN assist_primary_device dinfo ON pinfo.place_id = dinfo.place_id
        INNER JOIN ele_primary_device_type dtype ON dtype.primary_device_type_id = dinfo.type_id
        <where>
            <if test="params.deviceProperties==1">
                and (dtype.type_level=1 or dtype.type_level=2)
            </if>
            <if test="params.deviceProperties==2">
                and (dtype.type_level=3)
            </if>
            <if test="params.placeId!=null">
                and (pinfo.place_id=#{params.placeId} or pinfo.parent_id=#{params.placeId})
            </if>
        </where>

    </select>

    <select id="listTree" resultMap="deviceTreeEntity">
        SELECT ap.id c_t_id, ap.name c_t_name,
        apd.id, apd.name,apd.place_id
        from assist_primary_device apd
        LEFT JOIN assist_primary_device_parts apdp on apdp.from_parimary_deivce_id=apd.id
        left join assist_parts ap on apdp.parts_id=ap.id
        <where>
            <if test="placeId != null">apd.place_id=#{placeId}</if>
        </where>
    </select>
</mapper>



注意事项

  • 返回的数据结构是列表,并且列表的元素也包含需要自动聚合的其他实体列表数据时,mybatis的select标签需要用resultMap来承接返回的,并用来映射数据结构,resultType没有聚合数据的效果,即转不了一对多的层级数据列表结构
  • 正确示例:\<select id=“listTree” resultMap=“deviceTreeEntity”>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值