Mybatis 返回结果多重嵌套List

实际开发中,在定义接口的返回格式时往往会遇到返回json多重嵌套的情况:

  • 我们可以通过resultMap自动映射来实现
    
        
        
            
            
                
                
                
            
        
    

比如这样的resultMap结构就实现了三层嵌套,我们要注意创建PersonPosGroup、PersonData实体类来完成映射。

  • PersonPosGroup
public class PersonPosGroup implements Serializable {

    private String cameraId;
    private List personData;

    public String getCameraId() {
        return cameraId;
    }

    public void setCameraId(String cameraId) {
        this.cameraId = cameraId;
    }

    public List getPersonData() {
        return personData;
    }

    public void setPersonData(List 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代码

查询数据会根据层级关系自动映射出查询结果

你可能感兴趣的:(Mybatis,mybatis,mysql,spring,boot,java,后端)