简介
子查询:一个实体类中有许多孩子(自己)
上路
-
实体类(最好新建一个返回类)
public class DepartmentQueryDto { private Long deptId; /** * 子部门 */ private List<DepartmentQueryDto> children; public List<DepartmentQueryDto> getChildren() { return children; } public void setChildren(List<DepartmentQueryDto> children) { this.children = children; } public Long getDeptId() { return deptId; } public void setDeptId(Long deptId) { this.deptId = deptId; } }
-
dao
// 查询全部树 List<DepartmentQueryDto> findTree(); // 询指定父id的所有树 List<DepartmentQueryDto> findTreeByParentId();
-
mapper.xml
<resultMap id="childrenResultMap" type="com.xquant.system.manager.department.dto.DepartmentQueryDto"> <id property="deptId" column="DEPT_ID" ></id> <collection property="children" column="DEPT_ID" select="findTreeByParentId"/> </resultMap> <select id="findTree" resultMap="childrenResultMap"> select DEPT_ID from TP_SYS_DEPARTMENT where PARENT_DEPT_ID = 0 </select> <select id="findTreeByParentId" resultMap="childrenResultMap"> select DEPT_ID from TP_SYS_DEPARTMENT where PARENT_DEPT_ID = #{DEPT_ID} </select>