准备
课程树状数据查询 , 为CourseType增加children字段
//用来查询树状结构的 课程类型列表
@TableField(exist = false)
private List<CourseType> children = new ArrayList<>();
1.service实现类代码
courseTypes = baseMapper.selectForTreeData();
2.mapper层
//第1条:根据pid=0或pid=xxx查询出第1级课程分类列表
List<CourseType> selectForTreeData();
3.mapper.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.x.x.mapper.CourseTypeMapper">
<select id="selectForTreeData" resultMap="BaseResultMap">
SELECT *
FROM t_course_type where pid = 0
</select>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cn.x.x.domain.CourseType">
<id column="id" property="id" />
<!--嵌套子查询:查询当前分类下的子分类列表 :1037
下面的sql:cn.itsource.hrm.mapper.CourseTypeMapper.selectByPid 是重点。相当于在查询返回的结果
是当前map类型时,它会自动将查询出来的每一条数据的column="id"作为查询条件,来执行select语句
property :对应的 type="cn.x.x.domain.CourseType"中的属性值。
将collection查询出来的结果 ,保存到 CourseType中的指定属性中
ofType : 当前collection执行之后,返回的集合中的对象类型
column : 表中的字段的值作为select的查询条件
select : 当前collection将要执行的sql语句(支持2种传入方式:1,Sql语句;2:当前mapper.xml文件中的某一个select的id)
-->
<collection property="children"
ofType="cn.x.x.domain.CourseType"
column="id"
select="cn.itsource.th.mapper.CourseTypeMapper.selectByPid" />
</resultMap>
<!--根据父分类id,查询列表-->
<select id="selectByPid" resultMap="BaseResultMap">
SELECT *
FROM t_course_type where pid = #{pid}
</select>
</mapper>