实体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() {
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);
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”>