开发流程
需求分析
配置课时(课程内容管理)模块,主要是对课程内容进行管理。
数据库表分析
course - 课程表
course_section - 课程章节表
course_lesson - 课时信息表
一个课程表对多个课程章节表,一个课程章节表对多个课时表。
实体类设计
Course 类与 Course_Section 类是一对多关系;Course_Section 类与 Course_Lesson 类是一对多关系。
在 Course 类中定义一个 List 集合,并指定 List 的泛型是 Course_Section 类型,表示 一个课程中可以包含多个章节。
在 Course_Section 类中,定义一个 Course 类型的属性,用来保存章节所对应的具体的课程信息。
在 Course_Section 类中定义一个 List 集合,并指定 List 的泛型是 Course_Lesson 类型,这样就可以表示一个章节中包含多个课时。
// Course 类:
...
List<Course_Section> sectionList = new ArrayList<>();
...
// Course_Section 类:
...
List<Course_Lesson> lessonList = new ArrayList<>();
private Course course;
...
// Course_Lesson 类:
...
private Course_Section course_section;
...
Dao 接口及实现类编写
/**
* 课程内容管理 DAO 层接口
* */
public interface CourseContentDao {
}
/**
* 课程内容管理 DAO 层实现类
* */
public class CourseContentDaoImpl implements CourseContentDao {
}
Service 接口及实现类编写
/**
* 课程内容管理 Service 层接口
* */
public interface CourseContentService {
}
/**
* 课程内容管理 Service 层实现类
* */
public class CourseContentServiceImpl implements CourseContentService {
}
CourseContentServlet
编写
CourseContentServlet
继承 BaseServlet
@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
}
功能一:展示课程内容
需求分析
分析:要展示的内容是对应课程下的章节与课时信息
-- 查询 ID 为 1 的课程的章节与课时信息
SELECT
cs.`id`,
cs.`section_name`,
cl.`theme` '课时名称'
FROM course_section cs INNER JOIN course_lesson cl
ON cs.`id` = cl.`section_id`
WHERE cs.`course_id` = 59;
-- 在程序中尽量避免使用连接查询。可以将上面的 SQL 进行拆分,每一条 SQL 对应一个功能
-- 根据课程 ID 查询章节相关的内容
SELECT
id,
course_id,
section_name,
description,
order_num,
`status`
FROM course_section WHERE course_id = 59;
-- 根据章节 ID 查询课时相关的内容
SELECT
id,
course_id,
section_id,
theme,
is_free,
order_num,
`status`
FROM course_lesson WHERE section_id = 32;
DAO 层编写
编写两个方法
CourseContentDaoImpl
/**
* 根据课程 ID 查询课程相关信息
*
* @param courseId
*/
@Override
public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {
try {
// 创建 QueryRunner
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
// 编写 SQL
String sql = "SELECT \n" +
"id,\n" +
"course_id,\n" +
"section_name,\n" +
"description,\n" +
"order_num,\n" +
"STATUS\n" +
"FROM course_section WHERE course_id = ?";
// 执行查询
List<Course_Section> sectionList = qr.query(sql,
new BeanListHandler<Course_Section>(Course_Section.class), courseId);
// 根据章节 ID 查询课时信息
for (Course_Section section : sectionList) {
// 调用获取章节对应的课时方法,将课时数据封装到章节对象中
section.setLessonList(findLessonBySectionId(section.getId()));
}
// 返回结果
return sectionList;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
/**
* 根据章节 ID 查询章节相关的课时信息
*
* @param sectionId
*/
@Override
public List<Course_Lesson> findLessonBySectionId(int sectionId) {
try {
// 创建 QueryRunner
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
// 编写 SQL
String sql = "SELECT \n" +
"id,\n" +
"course_id,\n" +
"section_id,\n" +
"theme,\n" +
"duration,\n" +
"is_free,\n" +
"order_num,\n" +
"STATUS\n" +
"FROM course_lesson WHERE section_id = ?";
// 执行查询
return qr.query(sql, new BeanListHandler<Course_Lesson>(Course_Lesson.class), sectionId);
} catch (SQLException thro