您好,登錄后才能下訂單哦!
小編給大家分享一下Java中如何實現遞歸查詢部門樹形結構數據,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
說明:在開發中,我們經常使用樹形結構來展示菜單選項,如圖:
那么我們在后端怎么去實現這樣的一個功能呢?
1、數據庫表:department
2、編寫sql映射語句
<select id="selectDepartmentTrees" resultType="com.welb.entity.Department"> select * from department <where> <if test="updepartmentcode!=null"> and UpDepartmentCode=#{updepartmentcode} </if> </where> </select>
3、創建實體類
public class Department { private String departmentcode;//部門code private String departmentname;//部門名稱 private String updepartmentcode="0";//上級部門 private String departmentdesc;//部門描述 private List<Department> 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<Department> getChildDept() { return childDept; } public void setChildDept(List<Department> childDept) { this.childDept = childDept; } }
4、定義mapper
@Mapper public interface DepartmentMapper { List<Department>selectDepartmentTrees(Department department); }
5、定義service業務層實現
@Service @Transactional public class DepartmentImpl implements IDepartmentService { @Resource DepartmentMapper departmentMapper; @Override public List<Department> 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<Department> 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<Department> getDepartmentList(Department department) { List<Department> departments =departmentService.selectDepartmentTrees(department); if (departments.size()>0){ Department department1=new Department(); for (int i = 0; i <departments.size() ; i++) { department1.setUpdepartmentcode(departments.get(i).getDepartmentcode()); List<Department>dtos=getDepartmentList(department1); departments.get(i).setChildDept(dtos); } } return departments; } }
7、進行測試,這里我用postmain工具測試,結果如下圖
看完了這篇文章,相信你對“Java中如何實現遞歸查詢部門樹形結構數據”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。