创建一个基于SSM(Spring, Spring MVC, MyBatis)的教学视频点播系统

开发指南:包括项目结构、数据库设计、配置文件、DAO层、Service层、Controller层和前端页面的示例。

1. 需求分析

明确系统的主要功能需求,例如:

  • 用户注册与登录
  • 视频上传与管理
  • 视频播放与评论
  • 分类管理
  • 搜索功能
  • 用户权限管理

2. 技术选型

确定使用的技术栈:

  • 后端:Spring, Spring MVC, MyBatis
  • 前端:HTML, CSS, JavaScript (可选框架如Vue.js或React.js)
  • 数据库:MySQL
  • 服务器:Tomcat

3. 数据库设计

设计数据库模型,比如用户表、视频表、分类表等。这里以用户表、视频表和分类表为例:

user
sql 复制代码
CREATE TABLE `user` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `username` VARCHAR(50) NOT NULL UNIQUE,
  `password` VARCHAR(100) NOT NULL,
  `email` VARCHAR(100),
  `phone` VARCHAR(20),
  `role` VARCHAR(50) DEFAULT 'user',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);
video
sql 复制代码
CREATE TABLE `video` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `title` VARCHAR(255) NOT NULL,
  `description` TEXT,
  `file_path` VARCHAR(255) NOT NULL,
  `category_id` INT NOT NULL,
  `upload_user_id` INT NOT NULL,
  `upload_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`category_id`) REFERENCES `category`(`id`),
  FOREIGN KEY (`upload_user_id`) REFERENCES `user`(`id`)
);
category
sql 复制代码
CREATE TABLE `category` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(100) NOT NULL UNIQUE,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP
);

4. 创建项目结构

使用IDE(如IntelliJ IDEA或Eclipse)创建一个新的Maven项目,并添加必要的依赖项到pom.xml文件中。

5. 配置Spring和MyBatis

src/main/resources目录下创建配置文件,如applicationContext.xmlmybatis-config.xml,用于配置Spring和MyBatis。

applicationContext.xml 示例
xml 复制代码
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/video?useSSL=false&serverTimezone=UTC"/>
    <property name="username" value="root"/>
    <property name="password" value="password"/>
</bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.video.mapper"/>
</bean>

6. 编写Mapper接口

定义MyBatis的Mapper接口来操作数据库。例如,为用户表、视频表和分类表创建UserMapper.javaVideoMapper.javaCategoryMapper.java

UserMapper.java
java 复制代码
package com.video.mapper;

import com.video.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    @Insert("INSERT INTO user(username, password, email, phone, role) VALUES(#{username}, #{password}, #{email}, #{phone}, #{role})")
    int insert(User user);
}
VideoMapper.java
java 复制代码
package com.video.mapper;

import com.video.entity.Video;
import org.apache.ibatis.annotations.*;

@Mapper
public interface VideoMapper {
    @Select("SELECT * FROM video")
    List<Video> findAll();

    @Select("SELECT * FROM video WHERE id = #{id}")
    Video findById(@Param("id") int id);

    @Insert("INSERT INTO video(title, description, file_path, category_id, upload_user_id) VALUES(#{title}, #{description}, #{file_path}, #{category_id}, #{upload_user_id})")
    int insert(Video video);

    @Update("UPDATE video SET title=#{title}, description=#{description}, file_path=#{file_path}, category_id=#{category_id}, upload_user_id=#{upload_user_id} WHERE id=#{id}")
    int update(Video video);

    @Delete("DELETE FROM video WHERE id=#{id}")
    int delete(@Param("id") int id);
}
CategoryMapper.java
java 复制代码
package com.video.mapper;

import com.video.entity.Category;
import org.apache.ibatis.annotations.*;

@Mapper
public interface CategoryMapper {
    @Select("SELECT * FROM category")
    List<Category> findAll();

    @Select("SELECT * FROM category WHERE id = #{id}")
    Category findById(@Param("id") int id);

    @Insert("INSERT INTO category(name) VALUES(#{name})")
    int insert(Category category);

    @Update("UPDATE category SET name=#{name} WHERE id=#{id}")
    int update(Category category);

    @Delete("DELETE FROM category WHERE id=#{id}")
    int delete(@Param("id") int id);
}

7. 实现Service层

编写服务层来处理业务逻辑。例如,创建一个UserService.javaVideoService.javaCategoryService.java

UserService.java
java 复制代码
package com.video.service;

import com.video.entity.User;
import com.video.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    public void register(User user) {
        userMapper.insert(user);
    }
}
VideoService.java
java 复制代码
package com.video.service;

import com.video.entity.Video;
import com.video.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoService {
    @Autowired
    private VideoMapper videoMapper;

    public List<Video> getAllVideos() {
        return videoMapper.findAll();
    }

    public Video getVideoById(int id) {
        return videoMapper.findById(id);
    }

    public void addVideo(Video video) {
        videoMapper.insert(video);
    }

    public void updateVideo(Video video) {
        videoMapper.update(video);
    }

    public void deleteVideo(int id) {
        videoMapper.delete(id);
    }
}
CategoryService.java
java 复制代码
package com.video.service;

import com.video.entity.Category;
import com.video.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public List<Category> getAllCategories() {
        return categoryMapper.findAll();
    }

    public Category getCategoryById(int id) {
        return categoryMapper.findById(id);
    }

    public void addCategory(Category category) {
        categoryMapper.insert(category);
    }

    public void updateCategory(Category category) {
        categoryMapper.update(category);
    }

    public void deleteCategory(int id) {
        categoryMapper.delete(id);
    }
}

8. 控制器层

使用Spring MVC编写控制器来处理HTTP请求。例如,创建一个UserController.javaVideoController.javaCategoryController.java

UserController.java
java 复制代码
package com.video.controller;

import com.video.entity.User;
import com.video.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.login(username, password);
        if (user != null) {
            return "redirect:/home";
        } else {
            return "login";
        }
    }

    @PostMapping("/register")
    public String register(@ModelAttribute User user) {
        userService.register(user);
        return "redirect:/login";
    }
}
VideoController.java
java 复制代码
package com.video.controller;

import com.video.entity.Video;
import com.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @GetMapping("/list")
    public String listVideos(Model model) {
        List<Video> videos = videoService.getAllVideos();
        model.addAttribute("videos", videos);
        return "videoList";
    }

    @GetMapping("/view/{id}")
    public String viewVideo(@PathVariable("id") int id, Model model) {
        Video video = videoService.getVideoById(id);
        model.addAttribute("video", video);
        return "videoView";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("video", new Video());
        return "videoAdd";
    }

    @PostMapping("/add")
    public String addVideo(@ModelAttribute Video video) {
        videoService.addVideo(video);
        return "redirect:/video/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") int id, Model model) {
        Video video = videoService.getVideoById(id);
        model.addAttribute("video", video);
        return "videoEdit";
    }

    @PostMapping("/edit/{id}")
    public String editVideo(@PathVariable("id") int id, @ModelAttribute Video video) {
        video.setId(id);
        videoService.updateVideo(video);
        return "redirect:/video/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteVideo(@PathVariable("id") int id) {
        videoService.deleteVideo(id);
        return "redirect:/video/list";
    }
}
CategoryController.java
java 复制代码
package com.video.controller;

import com.video.entity.Category;
import com.video.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public String listCategories(Model model) {
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("categories", categories);
        return "categoryList";
    }

    @GetMapping("/view/{id}")
    public String viewCategory(@PathVariable("id") int id, Model model) {
        Category category = categoryService.getCategoryById(id);
        model.addAttribute("category", category);
        return "categoryView";
    }

    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("category", new Category());
        return "categoryAdd";
    }

    @PostMapping("/add")
    public String addCategory(@ModelAttribute Category category) {
        categoryService.addCategory(category);
        return "redirect:/category/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditForm(@PathVariable("id") int id, Model model) {
        Category category = categoryService.getCategoryById(id);
        model.addAttribute("category", category);
        return "categoryEdit";
    }

    @PostMapping("/edit/{id}")
    public String editCategory(@PathVariable("id") int id, @ModelAttribute Category category) {
        category.setId(id);
        categoryService.updateCategory(category);
        return "redirect:/category/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteCategory(@PathVariable("id") int id) {
        categoryService.deleteCategory(id);
        return "redirect:/category/list";
    }
}

9. 前端页面

根据需要设计前端页面,可以使用Thymeleaf作为模板引擎。例如,创建一个简单的登录页面login.html和视频列表页面videoList.html

login.html
html 复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Login Page</title>
</head>
<body>
<form th:action="@{/user/login}" method="post">
    <label>Username:</label><input type="text" name="username"/><br/>
    <label>Password:</label><input type="password" name="password"/><br/>
    <button type="submit">Login</button>
</form>
</body>
</html>
videoList.html
html 复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Video List</title>
</head>
<body>
<h1>Video List</h1>
<table border="1">
    <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Description</th>
        <th>File Path</th>
        <th>Category</th>
        <th>Upload User</th>
        <th>Upload Time</th>
        <th>Actions</th>
    </tr>
    <tr th:each="video : ${videos}">
        <td th:text="${video.id}"></td>
        <td th:text="${video.title}"></td>
        <td th:text="${video.description}"></td>
        <td th:text="${video.file_path}"></td>
        <td th:text="${video.category_id}"></td>
        <td th:text="${video.upload_user_id}"></td>
        <td th:text="${video.upload_time}"></td>
        <td>
            <a th:href="@{/video/view/{id}(id=${video.id})}">View</a>
            <a th:href="@{/video/edit/{id}(id=${video.id})}">Edit</a>
            <a th:href="@{/video/delete/{id}(id=${video.id})}">Delete</a>
        </td>
    </tr>
</table>
<a href="/video/add">Add New Video</a>
</body>
</html>

10. 测试与部署

完成所有编码后,进行单元测试确保各部分工作正常。之后,可以将应用部署到Tomcat服务器上。

相关推荐
张保瑞3 分钟前
十一:java web(3)-- Spring框架 -- Spring简介
java·前端·spring
coding已疯狂7 分钟前
面试官:说一下SpringBoot 启动流程
java·spring boot·spring
毕业设计制作和分享10 分钟前
ssm公交车信息管理系统+vue
java·vue.js·spring boot·毕业设计·mybatis
( •̀∀•́ )92021 分钟前
使用 Spring Boot 集成 Thymeleaf 和 Flying Saucer 实现 PDF 导出
spring boot·spring·pdf
爱敲代码的小冰39 分钟前
java mapper 的 xml讲解
xml·java·mybatis
李少兄1 小时前
使用Mybatis-plus出现数据库id很大或者为负数情况排查解决
数据库·oracle·mybatis
wuyu11251 小时前
Qt字符编码
数据库·mysql·mybatis
麻衣带我去上学2 小时前
Spring源码学习(五):Spring AOP
java·学习·spring
昂子的博客4 小时前
通过mybatis和mybatis plus 实现用户注册功能和基础的增删改查
java·开发语言·mybatis
烂漫心空4 小时前
Spring Boot 整合 MyBatis
java·spring boot·maven·mybatis