面包屑的查询

看你前台需要什么样子的数据一般来说是一个包含兄弟节点的数据

/**
 * 做面包屑的查询,因为前端需要的数据为
 * [{
 *    ownerProductType;{课程类型}
 *    otherProductTypes:[{课程}]
 * }]
 * 所以我们需要封装一个字段
 */
public class CourseTypeCrumbsDto {
    //该节点
    private CourseType ownerProductType;
    //兄弟节点
    private List<CourseType> otherProductTypes = new ArrayList<CourseType>();
}
View Code

然后我们前端发一个请求过来

 @RequestMapping(value="/crumbs/{id}",method= RequestMethod.GET)
    public AjaxResult crumbs(@PathVariable("id")Long id){
        try {
            List<CourseTypeCrumbsDto> crumbsDtos = courseTypeService.crumbs(id);
            return AjaxResult.me().setResultObj(crumbsDtos);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败!"+e.getMessage());
        }
    }
View Code

然后就是我们的service其实很简单这里我们把所以父节点及自己的节点存入PATH这个位置这样我们就可以拿到自己的父节点了

通过父节点查询他们的兄弟节点就可以实现面包屑

@Override
    public List<CourseTypeCrumbsDto> crumbs(Long id) {
        //1.通过id找到Path中的目录
        CourseType courseType = baseMapper.selectById(id);
        String path = courseType.getPath();
        String[] paths = path.split("\.");
        //查询所有的paths的节点
        List<CourseType> courseTypes = baseMapper.selectBatchIds(Arrays.asList(paths));
        //保存面包屑的值
        List<CourseTypeCrumbsDto> crumbsDtos = new ArrayList<CourseTypeCrumbsDto>();
        courseTypes.forEach(curentCourseType->{
            CourseTypeCrumbsDto courseTypeCrumbsDto = new CourseTypeCrumbsDto();
            courseTypeCrumbsDto.setOwnerProductType(curentCourseType);
            //找到和自己一样父ID的节点写个方法需要自己写
            List<CourseType> brothers = baseMapper.selectBrotherByPid(curentCourseType.getPid());
            courseTypeCrumbsDto.setOtherProductTypes(brothers);
            crumbsDtos.add(courseTypeCrumbsDto);
        });
        return crumbsDtos;
    }
View Code
原文地址:https://www.cnblogs.com/xiaoruirui/p/13673740.html