假设数据库中部门表如下:
部门id | 部门名称 | 上级部门id |
---|---|---|
1 | a | 0 |
2 | b | 1 |
3 | c | 7 |
4 | d | 1 |
5 | e | 2 |
6 | f | 5 |
7 | g | 1 |
8 | h | 9 |
9 | i | 7 |
10 | j | 8 |
假设顶级部门的上级部门为0。
现在我们想要将上述表结构转换为下面的json传递给前端,该如何转换呢?
{
"name":"a",
"subDeptList":[
{
"name":"b",
"subDeptList":[
{
"name":"e",
"subDeptList":[
{
"name":"f",
"subDeptList":[
]
}
]
}
]
},
{
"name":"d",
"subDeptList":[
]
},
{
"name":"g",
"subDeptList":[
{
"name":"c",
"subDeptList":[
]
},
{
"name":"i",
"subDeptList":[
{
"name":"h",
"subDeptList":[
{
"name":"j",
"subDeptList":[
]
}
]
}
]
}
]
}
]
}
首先准备我们数据库中表结构类型的domain对象:
package per.yushang.domain;
public class DBDepartment {
private String id;
private String name;
private String superId;
public DBDepartment() {
}
public DBDepartment(String id, String name, String superId) {
this.id = id;
this.name = name;
this.superId = superId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSuperId() {
return superId;
}
public void setSuperId(String superId) {
this.superId = superId;
}
}
接着再准备我们想传递给前端的数据类型vo:
package per.yushang.domain;
import java.util.List;
public class Department {
private String name;// 部门名称
private List<Department> subDeptList;// 子部门集合
public Department(String name, List<Department> subDeptList) {
this.name = name;
this.subDeptList = subDeptList;
}
public Department() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Department> getSubDeptList() {
return subDeptList;
}
public void setSubDeptList(List<Department> subDeptList) {
this.subDeptList = subDeptList;
}
@Override
public String toString() {
return "Department{" +
"name='" + name + '\'' +
", subDeptList=" + subDeptList +
'}';
}
}
因为懒得连数据库表了,这里用工具类模拟数据库查询到的部门列表:
package per.yushang.data;
import per.yushang.domain.DBDepartment;
import java.util.ArrayList;
import java.util.List;
public class PreparedData {
public static List<DBDepartment> getData(){
List<DBDepartment> depts = new ArrayList<>();
DBDepartment dept0 = new DBDepartment("1","a","0");
DBDepartment dept1 = new DBDepartment("2","b","1");
DBDepartment dept2 = new DBDepartment("3","c","7");
DBDepartment dept3 = new DBDepartment("4","d","1");
DBDepartment dept4 = new DBDepartment("5","e","2");
DBDepartment dept5 = new DBDepartment("6","f","5");
DBDepartment dept6 = new DBDepartment("7","g","1");
DBDepartment dept7 = new DBDepartment("8","h","9");
DBDepartment dept8 = new DBDepartment("9","i","7");
DBDepartment dept9 = new DBDepartment("10","j","8");
depts.add(dept0);
depts.add(dept1);
depts.add(dept2);
depts.add(dept3);
depts.add(dept4);
depts.add(dept5);
depts.add(dept6);
depts.add(dept7);
depts.add(dept8);
depts.add(dept9);
return depts;
}
}
最后就是运用递归进行转换:
package per.yushang.test;
import com.google.gson.Gson;
import per.yushang.data.PreparedData;
import per.yushang.domain.DBDepartment;
import per.yushang.domain.Department;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
/**
* 获取当前部门下一级的部门列表
* @param superId 当前部门的id
* @param dbDepartments 数据库查询到的部门列表
* @return 数据库表结构的子部门集合
*/
public List<DBDepartment> getSub(String superId,List<DBDepartment> dbDepartments){
List<DBDepartment> subList = new ArrayList<>();
// 遍历数据库查询到的部门列表,看看每个部门的上级部门id是不是传入的这个部门id
for(DBDepartment dbDepartment:dbDepartments){
if (dbDepartment.getSuperId().equals(superId)){
subList.add(dbDepartment);
}
}
return subList;
}
/**
* 获取数据库表中的顶级部门
* @param dbDepartments
* @return
*/
public DBDepartment findRoot(List<DBDepartment> dbDepartments){
for(DBDepartment dept:dbDepartments){
// 我们假设上级部门id为0的就是顶级部门
if (dept.getSuperId().equals("0")){
return dept;
}
}
return null;
}
/**
* 主要逻辑,递归在这
* @param root
* @param dbDepartments
* @return 返回的是所传入数据库表结构的部门所对应vo类型结构的部门
*/
public Department convert(DBDepartment root,List<DBDepartment> dbDepartments){
// 首先判断给的根部门是不是null,是null,我觉得你在逗我,我给你个null
if (root==null) return null;
// 获取传入根部门的子部门集合
List<DBDepartment> subList = getSub(root.getId(),dbDepartments);
// 构建当前这个根部门的vo数据
Department department = new Department();
department.setName(root.getName());
// 到这还需要设置根部门的子部门集合
List<Department> subs = new ArrayList<>();
// 如果集合不为空,则表明这个根部门有子部门
if (!subList.isEmpty()){
for (DBDepartment dbDepartment:subList){
// 递归
Department dd = convert(dbDepartment,dbDepartments);
subs.add(dd);
}
}
department.setSubDeptList(subs);
return department;
}
// 测试
public static void main(String[] args) {
TestMain testMain = new TestMain();
List<DBDepartment> dbDepartments = PreparedData.getData();
DBDepartment root = testMain.findRoot(dbDepartments);
Department dept = testMain.convert(root,dbDepartments);
System.out.println(dept);
Gson gson = new Gson();
String json = gson.toJson(dept);
System.out.println(json);
}
}