人生格言
未来犹存,人生当前
json 数据格式
[
{
id: 1,
label: "Level one 1",
children: [
{
id: 4,
label: "Level two 1-1",
children: [
{
id: 9,
label: "Level three 1-1-1",
},
{
id: 10,
label: "Level three 1-1-2",
},
],
},
],
},
{
id: 2,
label: "Level one 2",
children: [
{
id: 5,
label: "Level two 2-1",
},
{
id: 6,
label: "Level two 2-2",
},
],
},
{
id: 3,
label: "Level one 3",
children: [
{
id: 7,
label: "Level two 3-1",
},
{
id: 8,
label: "Level two 3-2",
},
],
},
]
实体类的准备
package com.gsx.eduservice.entity.subject;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class OneSubject {
private String id;
private String title;
private List<TwoSubject> children =new ArrayList<>();
}
package com.gsx.eduservice.entity.subject;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class TwoSubject {
private String id;
private String title;
}
整合代码的准备
@Override
public List<OneSubject> getAllSubject() {
QueryWrapper<EduSubject> oneWrapper =new QueryWrapper();
oneWrapper.eq("parent_id","0");
QueryWrapper<EduSubject> twoWrapper =new QueryWrapper();
twoWrapper.ne("parent_id","0");
List<EduSubject> oneSubjects=new ArrayList<>();
List<EduSubject> twoSubjects=new ArrayList<>();
oneSubjects=baseMapper.selectList(oneWrapper);
twoSubjects=baseMapper.selectList(twoWrapper);
List<OneSubject> subjects=new ArrayList<>();
for(int i=0;i<oneSubjects.size();i++){
OneSubject oneSubject=new OneSubject();
oneSubject.setId(oneSubjects.get(i).getId());
oneSubject.setTitle(oneSubjects.get(i).getTitle());
String pid1= oneSubjects.get(i).getId();
for (int j=0;j< twoSubjects.size();j++){
String pid2=twoSubjects.get(j).getParentId();
if(pid1.equals(pid2)){
TwoSubject twoSubject=new TwoSubject();
twoSubject.setId(twoSubjects.get(j).getId());
twoSubject.setTitle(twoSubjects.get(j).getTitle());
oneSubject.getChildren().add(twoSubject);
}
}
subjects.add(oneSubject);
}
return subjects;
}
整合代码简化版
@Override
public List<OneSubject> getAllSubject() {
QueryWrapper<EduSubject> oneWrapper =new QueryWrapper();
oneWrapper.eq("parent_id","0");
QueryWrapper<EduSubject> twoWrapper =new QueryWrapper();
twoWrapper.ne("parent_id","0");
List<EduSubject> oneSubjects=new ArrayList<>();
List<EduSubject> twoSubjects=new ArrayList<>();
oneSubjects=baseMapper.selectList(oneWrapper);
twoSubjects=baseMapper.selectList(twoWrapper);
List<OneSubject> subjects=new ArrayList<>();
for(int i=0;i<oneSubjects.size();i++){
OneSubject oneSubject=new OneSubject();
BeanUtils.copyProperties(oneSubjects.get(i),oneSubject);
String pid1= oneSubjects.get(i).getId();
for (int j=0;j< twoSubjects.size();j++){
String pid2=twoSubjects.get(j).getParentId();
if(pid1.equals(pid2)){
TwoSubject twoSubject=new TwoSubject();
BeanUtils.copyProperties(twoSubjects.get(j),twoSubject);
oneSubject.getChildren().add(twoSubject);
}
}
subjects.add(oneSubject);
}
return subjects;
}
整合结果
{
"success": true,
"code": 20000,
"message": "成功",
"data": {
"list": [
{
"id": "1522507649296162817",
"title": "前端开发",
"children": [
{
"id": "1522507649333911553",
"title": "vue"
},
{
"id": "1522507649333911554",
"title": "JavaScript"
},
{
"id": "1522507649401020417",
"title": "jquery"
}
]
},
{
"id": "1522507649401020418",
"title": "后端开发",
"children": [
{
"id": "1522507649468129281",
"title": "java"
},
{
"id": "1522507649468129282",
"title": "c++"
}
]
},
{
"id": "1522507649535238145",
"title": "数据库开发",
"children": [
{
"id": "1522507649535238146",
"title": "MySQL"
}
]
},
{
"id": "1522507671257538561",
"title": "中间件",
"children": [
{
"id": "1522507671320453122",
"title": "redis"
}
]
}
]
}
}
|