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 遞歸查詢部門樹形結構數據的實踐的文章就介紹到這瞭,更多相關Java 遞歸查詢樹形結構內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: