java用户里获取部门id_javaWeb开发,根据部门ID查询该部门和下属所有部门的ID集合...
由于博主太菜,就直接上圖了,結(jié)果是正確的。
1:首先看下數(shù)據(jù)庫
2:對應(yīng)的實體類
3:mapper.xml和mapper.java
再xml中根據(jù)限定條件查詢出當前所有的部門集合
4:在serviceImpl中做邏輯處理,最后用遞歸求出結(jié)果。
public class PostTotalNumServiceImpl implements PostTotalNumService {
/**
* 根據(jù)部門ID獲取該部門及其下屬部門的ID集合
*/
public List getDeptIdList(String deptId) {
Department department = this.getDepartment(deptId);
List deptIdList = new ArrayList<>();
if (department != null) {
deptIdList.add(department.getId());
deptIdList = addDeptIdList(deptIdList, department);
}
return deptIdList;
}
/**
* 根據(jù)部門ID獲取該部門及其下屬部門樹
*/
public Department getDepartment(String deptId) {
List departments = departmentMapper.findAllDepartments();
Map map = departments.stream().collect(
Collectors.toMap(Department::getId, department -> department));
for (Department dept : map.values()) {
Department parent = map.get(dept.getPid());
if (parent != null) {
List children = parent.getChildren() == null ? new ArrayList<>() : parent.getChildren();
children.add(dept);
parent.setChildren(children);
}
}
return map.get(deptId);
}
public List addDeptIdList(List deptIdList, Department department) {
List children = department.getChildren();
if (children != null) {
for (Department d : children) {
deptIdList.add(d.getId());
addDeptIdList(deptIdList, d);
}
}
return deptIdList;
}
}
5:service接口
public interface PostTotalNumService {
/**
*查出當前部門和下屬部門的集合
*/
List deptList(String pkDept);
}
6:junit單元測試
public class PostTest extends BaseTest {
@Autowired
private PostTotalNumService postService;
@Test
public void postTest(){
//查詢當前部門及下屬部門的集合
List strings = postService.deptList("1001K31000000003M1BT");
System.out.println(strings.size());
}
}
總結(jié)
以上是生活随笔為你收集整理的java用户里获取部门id_javaWeb开发,根据部门ID查询该部门和下属所有部门的ID集合...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java二分查找宿舍管理_1、任务:为宿
- 下一篇: 怎样把java换主题_如何为Eclips