在线学习平台的核心功能通常包括用户管理、课程管理、学习记录跟踪等模块,结合MySQL作为数据存储工具、Java作为后端开发语言,可以快速搭建出稳定可用的基础平台。下面将从整体设计到具体实现逐步讲解开发过程。

一、需求与整体架构设计
本次开发的简单在线学习平台需要实现三个核心功能:用户注册登录、课程信息增删改查、用户学习课程记录存储。整体架构采用前后端分离模式,后端使用Java的Spring Boot框架,结合MyBatis操作MySQL数据库,前端使用简单的HTML+JavaScript实现页面交互。
二、MySQL数据库设计
首先需要设计三张核心表,分别是用户表、课程表、学习记录表,表结构如下:
| 表名 | 字段说明 |
|---|---|
| user | 存储用户基本信息,包含id、用户名、密码、注册时间 |
| course | 存储课程信息,包含id、课程名称、课程简介、讲师、创建时间 |
| study_record | 存储用户学习记录,包含id、用户id、课程id、学习进度、最近学习时间 |
对应的建表SQL语句如下:
-- 创建用户表 CREATE TABLE user ( id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) NOT NULL UNIQUE, password VARCHAR(100) NOT NULL, register_time DATETIME DEFAULT CURRENT_TIMESTAMP ); -- 创建课程表 CREATE TABLE course ( id INT PRIMARY KEY AUTO_INCREMENT, course_name VARCHAR(100) NOT NULL, course_desc TEXT, teacher VARCHAR(50), create_time DATETIME DEFAULT CURRENT_TIMESTAMP ); -- 创建学习记录表 CREATE TABLE study_record ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, course_id INT NOT NULL, progress INT DEFAULT 0 COMMENT '学习进度,0-100表示百分比', last_study_time DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES user(id), FOREIGN KEY (course_id) REFERENCES course(id) );
三、Java后端核心模块实现
1. 环境搭建
首先创建Spring Boot项目,在pom.xml中添加MySQL驱动、MyBatis、Spring Web等依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
在application.yml中配置数据库连接信息:
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/study_platform?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.study.entity
2. 实体类定义
创建对应的实体类,与数据库表字段映射:
// 用户实体类
public class User {
private Integer id;
private String username;
private String password;
private Date registerTime;
// 省略getter和setter方法
}
// 课程实体类
public class Course {
private Integer id;
private String courseName;
private String courseDesc;
private String teacher;
private Date createTime;
// 省略getter和setter方法
}
// 学习记录实体类
public class StudyRecord {
private Integer id;
private Integer userId;
private Integer courseId;
private Integer progress;
private Date lastStudyTime;
// 省略getter和setter方法
}
3. 数据访问层实现
以课程管理为例,创建CourseMapper接口和对应的XML映射文件:
import com.example.study.entity.Course;
import java.util.List;
public interface CourseMapper {
// 新增课程
int addCourse(Course course);
// 查询所有课程
List<Course> getAllCourses();
// 根据id查询课程
Course getCourseById(Integer id);
// 更新课程信息
int updateCourse(Course course);
// 删除课程
int deleteCourse(Integer id);
}
对应的CourseMapper.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="com.example.study.mapper.CourseMapper">
<insert id="addCourse" parameterType="com.example.study.entity.Course">
INSERT INTO course (course_name, course_desc, teacher) VALUES (#{courseName}, #{courseDesc}, #{teacher})
</insert>
<select id="getAllCourses" resultType="com.example.study.entity.Course">
SELECT * FROM course ORDER BY create_time DESC
</select>
<select id="getCourseById" resultType="com.example.study.entity.Course">
SELECT * FROM course WHERE id = #{id}
</select>
<update id="updateCourse" parameterType="com.example.study.entity.Course">
UPDATE course SET course_name = #{courseName}, course_desc = #{courseDesc}, teacher = #{teacher} WHERE id = #{id}
</update>
<delete id="deleteCourse">
DELETE FROM course WHERE id = #{id}
</delete>
</mapper>
4. 业务层与控制层实现
创建课程服务的Service类和对应的Controller,提供接口给前端调用:
import com.example.study.entity.Course;
import com.example.study.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CourseService {
@Autowired
private CourseMapper courseMapper;
public int addCourse(Course course) {
return courseMapper.addCourse(course);
}
public List<Course> getAllCourses() {
return courseMapper.getAllCourses();
}
public Course getCourseById(Integer id) {
return courseMapper.getCourseById(id);
}
public int updateCourse(Course course) {
return courseMapper.updateCourse(course);
}
public int deleteCourse(Integer id) {
return courseMapper.deleteCourse(id);
}
}
import com.example.study.entity.Course;
import com.example.study.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService courseService;
@PostMapping("/add")
public String addCourse(@RequestBody Course course) {
int result = courseService.addCourse(course);
return result > 0 ? "添加成功" : "添加失败";
}
@GetMapping("/list")
public List<Course> getAllCourses() {
return courseService.getAllCourses();
}
@GetMapping("/get/{id}")
public Course getCourseById(@PathVariable Integer id) {
return courseService.getCourseById(id);
}
@PostMapping("/update")
public String updateCourse(@RequestBody Course course) {
int result = courseService.updateCourse(course);
return result > 0 ? "更新成功" : "更新失败";
}
@GetMapping("/delete/{id}")
public String deleteCourse(@PathVariable Integer id) {
int result = courseService.deleteCourse(id);
return result > 0 ? "删除成功" : "删除失败";
}
}
四、前端交互示例
前端可以通过简单的JavaScript调用后端接口,比如获取课程列表的示例:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>课程列表</title>
</head>
<body>
<h2>课程列表</h2>
<ul id="courseList"></ul>
<script>
fetch('http://127.0.0.1:8080/course/list')
.then(response => response.json())
.then(data => {
let list = document.getElementById('courseList');
data.forEach(course => {
let li = document.createElement('li');
li.textContent = course.courseName + ' - 讲师:' + course.teacher;
list.appendChild(li);
});
});
</script>
</body>
</html>
五、总结
通过以上步骤,我们就完成了一个简单在线学习平台的核心功能开发。实际开发中还可以扩展更多功能,比如课程视频上传、用户权限管理、学习进度统计报表等。整个开发过程中,MySQL负责数据的持久化存储,Java后端通过MyBatis操作数据库,提供标准化的接口,前端通过调用接口实现数据展示和交互,整体逻辑清晰,扩展性强。
MySQLJava在线学习平台Spring_BootMyBatis修改时间:2026-07-19 08:06:37