我曾遇到过的情况需要将多个DO模型转换成VO模型,由于DO模型中存在着的是ID集合,需要转换成VO中的集合,每一个ID集合都要进行一次批量查询,难免会有重复的ID,每一次查询都进行一次IO,消耗的时间是非常多的,(查询就会涉及到IO,有想了解的可以了解一下)所以我们可以保存要转换的所有的DO模型中的ID提取出来,保存为set集合,然后根据set集合的数据进行批量查询出所有的结果,保存到map集合中,键值对为 id :值,后面进行查询,就直接从集合中获取。
给出一个简单的例子:
下面是定义 DO 和 VO 的模型
public class StudentDO {
private String id;
private String studentName;
private String classId;
private List<String> courseIdList;
public StudentDO() {
}
public StudentDO(String id, String studentName, String classId, List<String> courseIdList) {
this.id = id;
this.studentName = studentName;
this.classId = classId;
this.courseIdList = courseIdList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public List<String> getCourseIdList() {
return courseIdList;
}
public void setCourseIdList(List<String> courseIdList) {
this.courseIdList = courseIdList;
}
@Override
public String toString() {
return "StudentDO{" +
"id='" + id + '\'' +
", studentName='" + studentName + '\'' +
", classId='" + classId + '\'' +
", courseIdList=" + courseIdList +
'}';
}
}
public class StudentVO {
private String id;
private String studentName;
private ClassVO classInfo;
private List<CourseVO> courseList;
public StudentVO() {
}
public StudentVO(String id, String studentName, ClassVO classInfo, List<CourseVO> courseList) {
this.id = id;
this.studentName = studentName;
this.classInfo = classInfo;
this.courseList = courseList;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public ClassVO getClassInfo() {
return classInfo;
}
public void setClassInfo(ClassVO classInfo) {
this.classInfo = classInfo;
}
public List<CourseVO> getCourseList() {
return courseList;
}
public void setCourseList(List<CourseVO> courseList) {
this.courseList = courseList;
}
@Override
public String toString() {
return "StudentVO{" +
"id='" + id + '\'' +
", studentName='" + studentName + '\'' +
", classInfo=" + classInfo +
", courseList=" + courseList +
'}';
}
}
具体的转换代码:先获取所有的id进行去重处理,进行批量查询放到map中
//获取courseVO
Set<String> courseIds = new HashSet<>(); // 将 id 进行去重并排序
for (StudentDO studentDO : studentList) {
List<String> courseIdList = studentDO.getCourseIdList();
courseIds.addAll(courseIdList);
}
List<String> courseIdList = new ArrayList<>(courseIds);
//去重之后依然能够获取所有的课程信息,因为包含了所有的id 后面虽然不能一一对应 但是可以通过 id 来一一对应
//listCourseByIds() 根据id集合来进行批量查询返回courseList
List<CourseVO> courseVOS = courseService.listCourseByIds(courseIdList);
//设置课程id和课程对应关系
Map<String, CourseVO> courseMap = new HashMap<>();
for (CourseVO courseVO : courseVOS) {
courseMap.put(courseVO.getId(), courseVO);
}
DO模型中根据id从map中获取 VO List
List<CourseVO> courseVOList = studentDO.getCourseIdList().stream().map(id -> courseMap.get(id)).collect(Collectors.toList());
此时就完成了操作,上述操作比起重复进行查询,或者是只用单一的查询,要节省很多的IO操作的时间,灵活使用map,虽然代码可能看起来变长了,但是效率变高了。