温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中如何实现递归查询部门树形结构数据

发布时间:2021-09-24 11:57:29 来源:亿速云 阅读:623 作者:小新 栏目:开发技术

小编给大家分享一下Java中如何实现递归查询部门树形结构数据,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

说明:在开发中,我们经常使用树形结构来展示菜单选项,如图:

Java中如何实现递归查询部门树形结构数据

那么我们在后端怎么去实现这样的一个功能呢?

1、数据库表:department

Java中如何实现递归查询部门树形结构数据

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中如何实现递归查询部门树形结构数据”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI