开发流程
需求分析
配置课时(课程内容管理)模块,主要是对课程内容进行管理。
数据库表分析
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 sectionList = new ArrayList<>();...// Course_Section 类:... List 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 clON 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 */@Overridepublic List findSectionAndLessonByCourseId(int courseId) { try { // 创建 QueryRunner QueryRunner qr = new QueryRunner(DruidUtils.getDataSource()); // 编写 SQL String sql = "SELECT " + "id," + "course_id," + "section_name," + "description," + "order_num," + "STATUS" + "FROM course_section WHERE course_id = ?"; // 执行查询 List sectionList = qr.query(sql, new BeanListHandler(Course_Section.class), courseId); // 根据章节 ID 查询课时信息 for (Course_Section section : sectionList) { // 调用获取章节对应的课时方法,将课时数据封装到章节对象中 section.setLessonList(findLessonBySectionId(section.getId())); } // 返回结果 return sectionList; } catch (SQLException t