Iterator与LIstIterator接口在java中的区别有哪些
616
2022-09-26
Java 递归查询部门树形结构数据的实践
说明:在开发中,我们经常使用树形结构来展示菜单选项,如图:
那么我们在后端怎么去实现这样的一个功能呢?
1、数据库表:department
2、编写sql映射语句
select * from department
and UpDepartmentCode=#{updepartmentcode}
3、创建实体类
public class Department {
private String departmentcode;//部门code
private String departmentname;//部门名称
private String updepartmentcode="0";//上级部门
private String departmentdesc;//部门描述
private List
public String getDepartmentcode() {
return departmentcode;
}
public void setDepartmentcode(String departmentcode) {
this.departmentcode = departmentcode == null ? null : departmentcode.trim();
}
public String getDepartmentname() {
return departmentname;
}
public void setDepartmentname(String departmentname) {
this.departmentname = departmentname == null ? null : departmentname.trim();
}
public String getUpdepartmentcode() {
return updepartmentcode;
}
public void setUpdepartmentcode(String updepartmentcode) {
this.updepartmentcode = updepartmentcode == null ? null : updepartmentcode.trim();
}
public String getDepartmentdesc() {
return departmentdesc;
}
public void setDepartmentdesc(String departmentdesc) {
this.departmentdesc = departmentdesc == null ? null : departmentdesc.trim();
}
public List
return childDept;
}
public void setChildDept(List
this.childDept = childDept;
}
}
4、定义mapper
@Mapper
public interface DepartmentMapper {
List
}
5、定义service业务层实现
@Service
@Transactional
public class DepartmentImpl implements IDepartmentService {
@Resource
DepartmentMapper departmentMapper;
@Override
public List
return departmentMapper.selectDepartmentTrees(department);
}
}
6、控制层实现
@RestController
@RequestMapping("department")
public class DepartmentController {
@Resource
IDepartNWsnNbmentService departmentService;
@Resource
IStationService stationService;
/**
* 查询所有部门 树形展示所有部门 包含模糊查詢
*
* @return
*/
@RequestMapping("/trehttp://elist")
public Object selectAll(Department department) {
ModelMap map = new ModelMap();
try {
List
map.put("msg", "查询部门成功");
map.put("data", departmentDtos);
map.put("code", 0);
} catch (Exception e) {
e.printStackTrace();
map.put("msg", "查询部门失败");
map.put("code", 1);
}
return map;
}
/**
* 递归查询部门
* @param department
* @return
*/
private List
List
if (departments.size()>0){
Department department1=new Department();
for (int i = 0; i department1.setUpdepartmentcode(departmeNWsnNbnts.get(i).getDepartmentcode()); List departments.get(i).setChildDept(dtos); } } return departments; } } 7、进行测试,这里我用postmain工具测试,结果如下图 到这里,递归查询部门树形结构数据就完成了
department1.setUpdepartmentcode(departmeNWsnNbnts.get(i).getDepartmentcode());
List
departments.get(i).setChildDept(dtos);
}
}
return departments;
}
}
7、进行测试,这里我用postmain工具测试,结果如下图
到这里,递归查询部门树形结构数据就完成了
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~