In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
小编给大家分享一下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 childDept =new ArrayList();//部门信息树 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 getChildDept() { return childDept; } public void setChildDept(List childDept) { this.childDept = childDept; }}
4、定义mapper
@Mapperpublic interface DepartmentMapper { ListselectDepartmentTrees(Department department); }
5、定义service业务层实现
@Service@Transactionalpublic class DepartmentImpl implements IDepartmentService { @Resource DepartmentMapper departmentMapper; @Override public List selectDepartmentTrees(Department department) { return departmentMapper.selectDepartmentTrees(department); }}
6、控制层实现
@RestController@RequestMapping("department")public class DepartmentController { @Resource IDepartmentService departmentService; @Resource IStationService stationService; /** * 查询所华岗门 树形展示所华岗门 包含模糊查询 * * @return */ @RequestMapping("/treelist") public Object selectAll(Department department) { ModelMap map = new ModelMap(); try { List departmentDtos = getDepartmentList(department); 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 getDepartmentList(Department department) { List departments =departmentService.selectDepartmentTrees(department); if (departments.size()>0){ Department department1=new Department(); for (int i = 0; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.