菜单树
以查询部门为例,查询的部门的所在菜单,有它的父级菜单和子级菜单,要求通过部门id直接查询该部门所在的部门菜单树
以Springboot为例子,该篇为学习笔记
/**根据部门id查询部门树**/
/**自定义的Service可自定义端口,这里我定义的部门树接口是DeptTree**/
@Service("DeptTree")
public List<Ztree> selectDept(Dept dept,SqlSession sqlSession) throws Exception{
/**查询前先给菜单树为空**/
List<Ztree> ztrees = null;
/**这里设置两个断言,dept和CommunityId是必传字段不能为空**/
Assert.notNull(dept,"部门不可为空");
Assert.notNull(dept.getCommunityId(),"小区id不可为空");
try{
/**打印前端传递来的参数**/
logger.info("开始查询菜单树,请求参数:{}",dept);
/**定义部门树集合,将DeptMapper下的属性放在list中**/
List<Dept> deptList = sqlSession.getMapper(DeptMapper.class).list(dept);
/**将放了属性的部门集合交给树**/
ztrees = initZtree(deptList);
}catch(Exception e){
logger.error("出现异常",e);
throw new Exception(CommConstant.ERROR_CODE,"查询菜单树异常");
}
return ztrees;
}
/**对象转部门树**/
private List<Ztree> initZtree(List<Dept> deptList){
List<Ztree> ztrees = new ArrayList<>();
for(Dept dept : deptList){
if(CommConstant.TATA_ACTIV_FALG.equals(dept.getStatusCd())){
Ztree ztrees = new Ztree();
/**在这里将获取的dept中的属性get出来放进新的部门树ztrees中**/
ztree.setId(dept.getDeptId());
ztree.setpId(dept.getParentId());
ztree.setName(dept.getName());
ztree.setTitle(dept.getLevel());
/**添加菜单树**/
ztrees.add(ztree)
}
}
return ztrees;
}