服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java 递归查询部门树形结构数据的实践

Java 递归查询部门树形结构数据的实践

2022-01-12 00:55哥的冷酷,你模仿不来 Java教程

本文主要介绍了Java 递归查询部门树形结构数据的实践,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

Java 递归查询部门树形结构数据的实践

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

1、数据库表:department

Java 递归查询部门树形结构数据的实践

2、编写sql映射语句

?
1
2
3
4
5
6
7
8
9
<select id="selectDepartmentTrees" resultType="com.welb.entity.Department">
       select * from department
       <where>
           <if test="updepartmentcode!=null">
               and UpDepartmentCode=#{updepartmentcode}
           </if>
          
       </where>
   </select>

3、创建实体类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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

?
1
2
3
4
@Mapper
public interface DepartmentMapper {
    List<Department>selectDepartmentTrees(Department department);
    }

5、定义service业务层实现

?
1
2
3
4
5
6
7
8
9
10
11
@Service
@Transactional
public class DepartmentImpl implements IDepartmentService {
    @Resource
    DepartmentMapper departmentMapper;
    
    @Override
    public List<Department> selectDepartmentTrees(Department department) {
        return departmentMapper.selectDepartmentTrees(department);
    }
}

6、控制层实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@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 递归查询部门树形结构数据的实践的文章就介绍到这了,更多相关Java 递归查询树形结构内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_42570879/article/details/90386962

延伸 · 阅读

精彩推荐