【郑州校区】学成在线 第6天 讲义-页面发布 课程管理七
3.2.3 API接口
3.2.3.1 数据模型
1、表结构
课程计划为树型结构,由树根(课程)和树枝(章节)组成,为了保证系统的可扩展性,在系统设计时将课程计划设置为树型结构。
[AppleScript] 纯文本查看 复制代码 @Data
@ToString
@Entity
@Table(name
=
"
teachplan
"
)
@GenericGenerator(name
=
"
jpa
‐
uuid
"
, strategy
=
"
uuid
"
)
public class Teachplan implements Serializable {
private static final long serialVersionUID =
‐
916357110051689485L;
@Id
@GeneratedValue(generator =
"
jpa
‐
uuid
"
)
@Column(length =
32)
private String id;
private String pname;
private String parentid;
private String grade;
private String ptype;
private String description;
private String courseid;
private String status;
private Integer orderby;
private Double timelength;
private String trylearn;
}
3.2.3.2 自定义模型类
前端页面需要树型结构的数据来展示Tree组件,如下:
[AppleScript] 纯文本查看 复制代码 [{
id: 1,
label:
'一
级 1
'
,
children: [{
id: 4,
label:
'
二级 1
‐
1
'
}]
}]
自定义课程计划结点类如下:
[AppleScript] 纯文本查看 复制代码 @Data
@ToString
public class TeachplanNode extends Teachplan {
List<TeachplanNode> children;
}
3.2.3.3 接口定义
根据课程id查询课程的计划接口如下,在api工程创建course包,创建CourseControllerApi接口类并定义接口方法如下:
[AppleScript] 纯文本查看 复制代码 public interface CourseControllerApi {
@ApiOperation(
"
课程计划查询
"
)
public TeachplanNode findTeachplanList(String courseId);
}
3.2.3 课程管理服务
3.2.3.1 Sql
课程计划是树型结构,采用表的自连接方式进行查询,sql语句如下:
[AppleScript] 纯文本查看 复制代码 SELECT
a.id one_id,
a.
pname one_pname,
b.id two_id,
b.
pname two_pname,
c.id three_id,
c.
pname three_pname
FROM
teachplan a
LEFT JOIN teachplan b
ON a.id
=
b.
parentid
LEFT JOIN teachplan c
ON b.id
=
c.
parentid
WHERE a.
parentid
=
'
0
'
AND a.courseid
=
'
402885816243d2dd016243f24c030002
'
ORDER BY a.orderby,
b.orderby,
c.orderby
3.2.3.2 Dao
1) mapper接口
[AppleScript] 纯文本查看 复制代码 @Mapper
public interface TeachplanMapper {
public TeachplanNode selectList(String courseId);
}
2)mapper映射文件
[AppleScript] 纯文本查看 复制代码 <resultMap type
=
"
com.xuecheng
.framework.domain.course.ext.TeachplanNode
"
id
=
"
teachplanMap
"
>
<id property
=
"
id
"
column=
"
one_id
"
/>
<result property
=
"
pname
"
column=
"
one_name
"
/>
<collection property
=
"
children
"
ofType
=
"
com.xuecheng
.framework.domain.course.ext.TeachplanNode
"
>
<id property
=
"
id
"
column=
"
two_id
"
/>
<result property
=
"
pname
"
column=
"
two_name
"
/>
<collection property
=
"
children
"
ofType
=
"
com.xuecheng
.framework.domain.course.ext.TeachplanNode
"
>
<id property
=
"
id
"
column=
"
three_id
"
/>
<result property
=
"
pname
"
column=
"
three_name
"
/>
</collection>
</collection>
</resultMap>
<select id
=
"
selectList
"
resultMap
=
"
teachplanMap
"
parameterType
=
"
java.lang
.String
"
>
SELECT
a.id one_id,
a.
pname one_name,
b.id two_id,
b.
pname two_name,
c.id three_id,
c.
pname three_name
FROM
teachplan a LEFT JOIN teachplan b
ON a.id
=
b.
parentid
LEFT JOIN teachplan c
ON b.id
=
c.
parentid
WHERE a.
parentid
=
'
0
'
<if test
=
"
_parameter!
=null and _parameter!
=
''"
>
and a.courseid
=#{courseId}
</if>
ORDER BY a.orderby,
b.orderby,
c.orderby
</select>
说明:针对输入参数为简单类型#{}中可以是任意类型,判断参数是否为空要用 _parameter(它属于mybatis的内置参数)
3.4.3.3 Service
创建CourseService类,定义查询课程计划方法。
[AppleScript] 纯文本查看 复制代码 @Service
public class CourseService {
@Autowired
TeachplanMapper teachplanMapper;
//查询课程计划
public TeachplanNode findTeachplanList(String courseId){
TeachplanNode teachplanNode
=
teachplanMapper.selectList(courseId);
return teachplanNode;
}
}
3.4.3.4 Controller
[AppleScript] 纯文本查看 复制代码 @RestController
@RequestMapping(
"
/course
"
)
public class CourseController implements CourseControllerApi {
@Autowired
CourseService courseService;
//查询课程计划
@Override
@GetMapping(
"
/teachplan/list/{courseId}
"
)
public TeachplanNode findTeachplanList(String courseId) {
return courseService.findTeachplanList(courseId);
}
}
3.4.3.5 测试
使用postman或swagger-ui测试查询接口。
Get 请求:http://localhost:31200/course/teachplan/list/402885816243d2dd016243f24c030002
|