一 创建vo
@Data
public class SubjectVo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 课程分类id
*/
private String id;
/**
* 标题
*/
private String title;
/**
* 排序
*/
private Integer sort;
/**
* 课程孩子节点
*/
private List<SubjectVo> children = new ArrayList<>();
}
二 控制器
/**
* @className: SubjectController
* @description: 课程分类列表
* @date: 2020/12/2
* @author: cakin
*/
@ApiOperation("嵌套数据列表")
@GetMapping("nested-list")
public R nestedList() {
List<SubjectVo> subjectVoList = subjectService.nestedList();
return R.ok().data("items", subjectVoList);
}
三 服务
1 接口
/**
* 功能描述:树形展示
*
* @author cakin
* @date 2020/12/2
* @return List<SubjectVo> 课程一级分类列表,包含嵌套关系
*/
List<SubjectVo> nestedList();
2 实现
/**
* 功能描述:课程分类列表展示
*
* @author cakin
* @date 2020/12/2
* @return List<SubjectVo> 课程一级分类列表,包含嵌套关系
*/
@Override
public List<SubjectVo> nestedList() {
return baseMapper.selectNestedListByParentId("0");
}
四 创建mapper映射
1 mapper
/**
* 功能描述:课程分类列表展示
*
* @author cakin
* @date 2020/12/2
* @return List<SubjectVo> 课程一级分类列表,包含嵌套关系
*/
List<SubjectVo> selectNestedListByParentId(String parentId);
2 xml
<mapper namespace="com.atguigu.guli.service.edu.mapper.SubjectMapper">
<resultMap id="nestedSubject" type="com.atguigu.guli.service.edu.entity.vo.SubjectVo">
<id property="id" column="id" />
<result property="title" column="title" />
<result property="sort" column="sort" />
<collection property="children"
column="id"
select="selectNestedListByParentId"
ofType="com.atguigu.guli.service.edu.entity.vo.SubjectVo" />
</resultMap>
<select id="selectNestedListByParentId" resultMap="nestedSubject">
select id, sort, title from edu_subject where parent_id = #{parentId}
</select>
</mapper>
五 pom配置,解决打包问题
<build>
<!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
六 Swagger 测试
![](https://i-blog.csdnimg.cn/blog_migrate/949579449905406905c788b9c380bbe7.png)