mybatis自关联查询树

mapper.java:

public List<DepartmentDTO> getTreeForm();

mapper.xml:

/*step1*/ /*step4*/...

<select id="getTreeForm" resultType="brilliance.znjt.business.postman.entity.DepartmentDTO"
                            resultMap="departmentDTOMap">
  SELECT DEPTNO,DEPTNAME,DEPTGRADE,PARENTDEPT 
    FROM VMGR_DEPT
    WHERE PARENTDEPT = '' OR PARENTDEPT IS NULL
  <!--  WHERE SUBSTR(DEPTNO,5) = '0000' -->
    ORDER BY DEPTGRADE,DEPTNO

  </select>

/*step2*//*step5*/...

  <resultMap type="brilliance.znjt.business.postman.entity.DepartmentDTO" id="departmentDTOMap">
        <id column="deptno" property="deptno"/>
        <result column="deptname" property="deptname"/>
        <result column="parentdept" property="parentdept"/>
        <result column="deptgrade" property="deptgrade"/>
        <collection property="children" ofType="brilliance.znjt.business.postman.entity.DepartmentDTO"
                      column="deptno" select="getChildByParentId"/>

    </resultMap>

/*step3*//*step6*/...

    <select id="getChildByParentId" resultType="brilliance.znjt.business.postman.entity.DepartmentDTO"
                                    resultMap="departmentDTOMap">
      SELECT DEPTNO,DEPTNAME,DEPTGRADE,PARENTDEPT 
    FROM VMGR_DEPT
     WHERE PARENTDEPT = #{deptno}
       <!-- AND SUBSTR(DEPTNO,5) != '0000' -->
    ORDER BY DEPTGRADE,DEPTNO

   </select>

service:

public Map<String, List<DepartmentDTO>> getTreeForm(){
List<DepartmentDTO> listMap=new ArrayList<>();
Map<String, List<DepartmentDTO>> map=new HashMap<>();
try {
listMap = this.resourcesMapper.getTreeForm();
map.put("data", listMap);
} catch (Exception e) {
log.error("getTreeForm---获取部门树表单错误");
log.error(e.getMessage());
}
return map;

}

controller:

@GetMapping("treeForm")
@ApiOperation("部门树形结构")
public Map<String, List<DepartmentDTO>> getTreeForm(){
return resourcesService.getTreeForm();

}

bean:

public class DepartmentDTO {

private String deptno;
private String deptname;
private String parentdept;
private String deptgrade;//char
private List<DepartmentDTO> children;

public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getParentdept() {
return parentdept;
}
public void setParentdept(String parentdept) {
this.parentdept = parentdept;
}
public String getDeptgrade() {
return deptgrade;
}
public void setDeptgrade(String deptgrade) {
this.deptgrade = deptgrade;
}
public List<DepartmentDTO> getChildren() {
return children;
}
public void setChildren(List<DepartmentDTO> children) {
this.children = children;
}
@Override
public String toString() {
return "DepartmentDTO [deptno=" + deptno + ", deptname=" + deptname + ", parentdept=" + parentdept
+ ", deptgrade=" + deptgrade + ", children=" + children + "]";
}

}

猜你喜欢

转载自blog.csdn.net/qq_36421955/article/details/80826496