表数据结构
扫描mapper.xml
开启mapper扫描
vo
@Data public class EduOneSubjectVO { @ApiModelProperty(value = "课程类别ID") private String id; @ApiModelProperty(value = "类别名称") private String label; @ApiModelProperty(value = "二级分类list内容") private List<EduOneSubjectVO> children = new ArrayList<>(); } mapper <mapper namespace="com.atguigu.eduservice.mapper.EduSubjectMapper"> <resultMap id="queryAllSubjectMap" type="com.atguigu.eduservice.entity.vo.EduOneSubjectVO"> <id property="id" column="oneId" /> <result property="label" column="oneTitle"/> <collection property="children" ofType="com.atguigu.eduservice.entity.vo.EduOneSubjectVO"> <id property="id" column="twoId"/> <result property="label" column="twoTitle"/> </collection> </resultMap> <select id="queryAllSubject" resultType="list" resultMap="queryAllSubjectMap"> SELECT o.id as oneId,o.title as oneTitle,t.id as twoId,t.title as twoTitle FROM edu_subject o LEFT JOIN edu_subject t on o.id = t.parent_id WHERE o.parent_id = 0 </select> service List<EduOneSubjectVO> queryAllSubject(); impl @Override public List<EduOneSubjectVO> queryAllSubject() { List<EduOneSubjectVO> eduOneSubjectVOS = subjectMapper.queryAllSubject(); Collections.sort(eduOneSubjectVOS, new Comparator<EduOneSubjectVO>() { @Override public int compare(EduOneSubjectVO o1, EduOneSubjectVO o2) { return o1.getChildren().size() - o2.getChildren().size() >= 0? -1:1; } }); return eduOneSubjectVOS; } controller @GetMapping("getAllSubject") @ApiOperation(value = "获取课程分类") public R getAllSubject(){ List<EduOneSubjectVO> list = subjectService.queryAllSubject(); return R.ok().data("list",list); }
目录结构
测试效果