实际开发中,在定义接口的返回格式时往往会遇到返回json多重嵌套的情况:
- 我们可以通过resultMap自动映射来实现
<resultMap type="PersonDetailsVo" id="personDetailsMap">
<result column="sequence_id" property="sequenceId" />
<collection property="personPosGroup" javaType="java.util.List" ofType="com.ruoyi.project.business.main.domain.PersonPosGroup">
<result column="camera_id" property="cameraId" />
<collection property="personData" javaType="java.util.List" ofType="com.ruoyi.project.business.main.domain.PersonData">
<result column="person_id" property="personId" />
<result column="position_info" property="positionInfo" />
<result column="worker_type" property="workerType" />
</collection >
</collection >
</resultMap>
比如这样的resultMap结构就实现了三层嵌套,我们要注意创建PersonPosGroup、PersonData实体类来完成映射。
- PersonPosGroup
public class PersonPosGroup implements Serializable {
private String cameraId;
private List<PersonData> personData;
public String getCameraId() {
return cameraId;
}
public void setCameraId(String cameraId) {
this.cameraId = cameraId;
}
public List<PersonData> getPersonData() {
return personData;
}
public void setPersonData(List<PersonData> personData) {
this.personData = personData;
}
}
- PersonData
public class PersonData implements Serializable {
private String personId;
private String workerType;
private JSONObject positionInfo;
public String getPersonId() {
return personId;
}
public void setPersonId(String personId) {
this.personId = personId;
}
public String getWorkerType() {
return workerType;
}
public void setWorkerType(String workerType) {
this.workerType = workerType;
}
public JSONObject getPositionInfo() {
return positionInfo;
}
public void setPositionInfo(String positionInfo) {
this.positionInfo = JSONObject.parseObject(positionInfo);
}
}
- sql代码
<select id="selectPersonInfoBySequenceId" parameterType="java.util.Map" resultMap="personDetailsMap">
select
sequence_id,
camera_id,
person_id,
position_info,
worker_type
from video_person_details_info
where
1=1
<if test="video_id != null and video_id != ''"> and video_id = #{video_id}</if>
<if test="start_sequence_id != null">
and sequence_id >= #{start_sequence_id} and #{end_sequence_id} > sequence_id
</if>
</select>
查询数据会根据层级关系自动映射出查询结果