导读:
1.项目环境:使用MyabtisPlus框架,代码利用了MybatisPlus自动生成代码工具开发环境:IntelliJ IDEA 2019.3.1 ,JDK1.8 ,MySQL: 5.6.47
采用远程数据库连接:使用ailiyun ESC CentOS 7.02.项目代码地址
https://github.com/WangLuLu0620/mybatis-plus-generator.git
一、问题引入
诸多项目中都会用到多表联结查询,尤其是父子表关联查询最为常见。但是当前端要求以list的格式返回的时候,你们都是用什么方法呢?
在之前我是用的迭代,虽然很方便,但是多个地方用到的时候就会感觉代码太冗余…
二、一般方法
方式一:使用for()迭代
public List<TeacherDTO> search(Long id){
List<TeacherDTO> repList = new ArrayList<>();
TeacherDTO TeacherDTO = new TeacherDTO();
//查询该老师的所有信息
Teacher teacher = getById(id);
BeanUtils.copyProperties(teacher,TeacherDTO);
repList.add(TeacherDTO);
//查询该老师的所有学生
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("teacher_id",id);
List<Student> students = studentService.list(queryWrapper);
//查询每个学生的个人信息
List<StudentDTO> StudentDTOList = new ArrayList<>();
for(Student student : students){
StudentDTO StudentDTO = new StudentDTO();
BeanUtils.copyProperties(student,StudentDTO);
StudentDTOList.add(StudentDTO);
TeacherDTO.setItemDTOS(StudentDTOList);
}
return repList;
}
方式二:单独使用java8特性 流
List<TeacherDto> repList = new ArrayList<>();
TeacherDto teacherDto = new TeacherDto();
//查询该老师的所有信息
Teacher teacher = teacherMapper.selectByPrimaryKey(id);
BeanUtils.copyProperties(teacher,teacherDto);
repList.add(teacherDto);
//查询该老师的所有学生
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("teacher_id",id);
List<Student> students = studentService.list(queryWrapper);
//查询每个学生的个人信息
List<StudentDto> list = students.stream().map(student -> {
StudentDto studentDto = new StudentDto();
BeanUtils.copyProperties(student, studentDto);
return studentDto;
}).collect(Collectors.toList());
teacherDto.setDtoList(list);
return repList;
}
- postman调用:
这两方式看上去思路很清晰,但是非常的麻烦,如果多出用到这种父子表关联的查询,会写很多的迭代,代码会显得非常的冗余,既然利用到了java8的stream流,我们可以依次创建一个配置类,作为公共方法让多处调用;
三、构建Bean增强类工具类
1.依赖
<!-- 提供丰富的Java工具方法,此模块为Hutool所有模块的打包汇总,最终形式为一个jar包 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.1.3</version>
</dependency>
2.Bean增强类工具类
/**
* Bean增强类工具类
*
* <p>
* 把一个拥有对属性进行set和get方法的类,我们就可以称之为JavaBean。
* </p>
*
* @author alvin
* @since 2020-02-21
*/
public class BeanPlusUtil extends BeanUtil {
/**
* 转换 list
*
* @param sourceList
* @param destinationClass
* @param <T>
* @param <E>
* @return
*/
public static <T, E> List<T> toBeanList(Collection<E> sourceList, Class<T> destinationClass) {
if (sourceList == null || sourceList.isEmpty() || destinationClass == null) {
return Collections.emptyList();
}
return sourceList.parallelStream()
.filter(item -> item != null)
.map((source) -> toBean(source, destinationClass))
.collect(Collectors.toList());
}
}
3.应用
在接口处调用:
@GetMapping("/myStudents")
public TeacherDTO get(@RequestParam Long id){
//查询该老师的所有信息
Teacher teacher = teacherService.getById(id);
//将老师信息拷贝到TeacherDTO对象
TeacherDTO teacherDTO = BeanPlusUtil.toBean(teacher, TeacherDTO.class);
//查询该老师的所有学生
QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("teacher_id",id);
List<Student> students = studentService.list(queryWrapper);
//将多个学生对象依次拷入StudentDTO对象
List<StudentDTO> studentDTOS = BeanPlusUtil.toBeanList(students, StudentDTO.class);
//将StudentDTO对象插入TeacherDTO对象
teacherDTO.setItemDTOS(studentDTOS);
return teacherDTO;
}
- postman
显然,对于学生对象我们无需一个一个的迭代拷贝,Bean增强类工具类已经帮助我们解决这样的问题,而且避免了在service实现类互相注入service,做到各司其职,不互相“串门”,只需要调用一下即可,减少了代码量,增强了可读性。