目录
1、创建DepartmentController
@Controller
public class DepartmentController {
@Autowired
private QuanXianDao quanXianDao;
@PostMapping("/hrmDepartment")
@ResponseBody
public List<Integer> getSubDepartments(@RequestParam("departmentId") Integer departmentId) {
//获取人员的部门ID
Integer useridDepartmentId = hrmDepartmentMapper.getHrmResource(departmentId);
List<Integer> subDepartmentIds = this.getSubDepartmentsRecursivelyE9(useridDepartmentId);
log.info("subDepartmentIds:{}", subDepartmentIds);
return subDepartmentIds;
}
//根据部门ID获取下级部门
public List<Integer> getSubDepartmentsRecursivelyE9(Integer departmentId) {
List<Integer> subDepartmentIds = new ArrayList<>();
subDepartmentIds.add(departmentId);
List<Map<String, Object>> currentDept = quanXianDao.getHrmDepartmentIdE9();
for (Map<String, Object> stringObjectMap : currentDept) {
Integer deptId = (Integer) stringObjectMap.get("id");
Integer parentId = (Integer) stringObjectMap.get("supdepid");
if (parentId != null && parentId.equals(departmentId)) {
subDepartmentIds.add(deptId);
getSubDepartmentsRecursivelyE9(deptId);
}
}
System.out.println(subDepartmentIds);
return subDepartmentIds;
}
}
2、创建QuanXianDao 接口
@Mapper
public interface QuanXianDao extends BaseMapper<Object> {
List<Map<String, Object>> getHrmDepartmentIdE9();
}
3、创建quanxianMapper.xml
supdepid代表部门表中的上级部门字段
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.****.QuanXianDao">
<resultMap id="BaseResultMap" type="java.util.Map">
</resultMap>
<select id="getHrmDepartmentIdE9" parameterType="int" resultType="java.util.Map">
SELECT id,supdepid from hrmdepartment
</select>
</mapper>