@Data
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class OrgTree implements Serializable {
private String id;
private String label;
private List<OrgTree> children;
}
<resultMap type="com.dto.OrgTree" id="BaseResultMap">
<id column="id" property="id"/>
<result column="label" property="label"/>
<collection property="children" ofType="com.dto.OrgTree">
<id column="id2" property="id"/>
<result column="label2" property="label"/>
<collection property="children" ofType="com.dto.OrgTree">
<id column="id3" property="id"/>
<result column="label3" property="label"/>
</collection>
</collection>
</resultMap>
<select id="getOrgTree" resultMap="BaseResultMap">
select
m1.org_code as id,
m1.org_name as label,
m2.org_code as id2,
m2.org_name as label2,
m3.org_code as id3,
m3.org_name as label3
from org_table m1,org_table m2,org_table m3
where
m1.org_code=m2.parent_org_code
and
m2.org_code=m3.parent_org_code
<!--可以根据不同用户的级别查询当前用户的组织-->
<if test="firstCode != '' and firstCode != null ">
and m1.org_code=#{firstCode}
</if>
<if test="secondCode != '' and secondCode != null ">
and m2.org_code=#{secondCode}
</if>
<if test="thirdCode != '' and thirdCode != null ">
and m3.org_code=#{thirdCode}
</if>
order by id,id2,id3
</select>
MyBatis - 两种查询树形数据的方法详解(嵌套结果集、递归查询)
参考连接