├── .gitignore ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── example │ └── demo │ ├── DemoApplication.java │ ├── annotation │ └── DisableBaseResponse.java │ ├── config │ └── CorsConfiguration.java │ ├── constant │ └── ArticleType.java │ ├── controller │ ├── ArticleController.java │ ├── CommentController.java │ ├── FileController.java │ ├── UserController.java │ └── VideoController.java │ ├── handler │ ├── GlobalExceptionHandler.java │ └── GlobalResponseHandler.java │ ├── mapper │ ├── ArticleMapper.java │ ├── CommentMapper.java │ ├── ImageMapper.java │ ├── UserMapper.java │ └── VideoMapper.java │ ├── model │ ├── entity │ │ ├── Article.java │ │ ├── Comment.java │ │ ├── Image.java │ │ ├── User.java │ │ └── Video.java │ ├── support │ │ └── ResponseResult.java │ └── vo │ │ ├── ArticleVo.java │ │ ├── CommentVo.java │ │ └── UserVo.java │ ├── service │ ├── ArticleService.java │ ├── CommentService.java │ ├── ImageService.java │ ├── UserService.java │ ├── VideoService.java │ └── impl │ │ ├── ArticleServiceImpl.java │ │ ├── CommentServiceImpl.java │ │ ├── ImageServiceImpl.java │ │ ├── UserServiceImpl.java │ │ └── VideoServiceImpl.java │ └── utils │ └── DataTimeUtil.java └── resources ├── application.yaml └── sql └── table.sql /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 知识社区 - 知否 2 | 3 | #### 前端仓库地址 https://github.com/soanr/know-app 4 | 5 | - SpringBoot + Vue 的前后端分离项目 6 | 7 | - Vant移动ui库 8 | 9 | #### 需要修改的地方 10 | 11 | - application.yaml的数据库相关配置 12 | 13 | - 数据库SQL resources/sql/table.sql 14 | 15 | - videoSavePath为保存在本地的路径 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.4.4 9 | 10 | 11 | com.example 12 | demo 13 | 0.0.1-SNAPSHOT 14 | 15 | 11 16 | 17 | 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | com.alibaba 27 | fastjson 28 | 1.2.73 29 | 30 | 31 | 32 | com.baomidou 33 | mybatis-plus-boot-starter 34 | 3.4.2 35 | 36 | 37 | 38 | mysql 39 | mysql-connector-java 40 | runtime 41 | 42 | 43 | 44 | org.projectlombok 45 | lombok 46 | true 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | org.projectlombok 60 | lombok 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class DemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(DemoApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/annotation/DisableBaseResponse.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * 作用: 不需要GlobalResponseHandler统一拦截返回JSON的方法 10 | * 例如: 获取二进制图片 Response需要写入文件流 不需要返回json 11 | */ 12 | @Target(ElementType.METHOD) 13 | @Retention(RetentionPolicy.RUNTIME) 14 | public @interface DisableBaseResponse { 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/config/CorsConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.servlet.config.annotation.CorsRegistry; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 6 | 7 | @Configuration 8 | public class CorsConfiguration extends WebMvcConfigurationSupport { 9 | 10 | @Override 11 | protected void addCorsMappings(CorsRegistry registry) { 12 | registry.addMapping("/**") 13 | .allowedHeaders("*") 14 | .allowedMethods("*") 15 | .allowedOrigins("*"); 16 | super.addCorsMappings(registry); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/constant/ArticleType.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.constant; 2 | 3 | public final class ArticleType { 4 | 5 | public static final String[] list = {"机器学习", "计算机", "工学", "农学", "物理", "高数", "哲学", "其他"}; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/controller/ArticleController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.constant.ArticleType; 4 | import com.example.demo.model.entity.Article; 5 | import com.example.demo.model.vo.ArticleVo; 6 | import com.example.demo.service.ArticleService; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | import javax.annotation.Resource; 10 | import java.util.List; 11 | 12 | @RestController 13 | @RequestMapping("/api/article") 14 | public class ArticleController { 15 | 16 | @Resource 17 | private ArticleService articleService; 18 | 19 | @GetMapping("/type/all") 20 | public String[] findAllType() { 21 | return ArticleType.list; 22 | } 23 | 24 | @GetMapping("/agree/{id}") 25 | public void agree(@PathVariable String id) { 26 | articleService.agree(id); 27 | } 28 | 29 | @GetMapping("/search/{value}") 30 | public List search(@PathVariable String value) { 31 | return articleService.search(value); 32 | } 33 | 34 | @PostMapping("") 35 | public Article save(@RequestBody Article article) { 36 | return articleService.save(article); 37 | } 38 | 39 | @GetMapping("") 40 | public List findAll(String type) { 41 | return articleService.findAll(type); 42 | } 43 | 44 | @GetMapping("/{id}") 45 | public ArticleVo findById(@PathVariable String id) { 46 | return articleService.findById(id); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/controller/CommentController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.model.entity.Comment; 4 | import com.example.demo.model.vo.CommentVo; 5 | import com.example.demo.service.CommentService; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import javax.annotation.Resource; 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/api/comment") 13 | public class CommentController { 14 | 15 | @Resource 16 | private CommentService commentService; 17 | 18 | @PostMapping("") 19 | public CommentVo save(@RequestBody Comment comment) { 20 | return commentService.save(comment); 21 | } 22 | 23 | @GetMapping("/article/{id}") 24 | public List findByArticle(@PathVariable String id) { 25 | return commentService.findByArticle(id); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/controller/FileController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.annotation.DisableBaseResponse; 4 | import com.example.demo.model.entity.Image; 5 | import com.example.demo.service.ImageService; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.web.bind.annotation.*; 8 | import org.springframework.web.multipart.MultipartFile; 9 | 10 | import javax.annotation.Resource; 11 | import javax.servlet.http.HttpServletResponse; 12 | import java.io.*; 13 | import java.util.UUID; 14 | 15 | @RestController 16 | @RequestMapping("/api/file") 17 | public class FileController { 18 | 19 | @Resource 20 | private ImageService imageService; 21 | 22 | @Value("${videoSavePath}") 23 | private String savePath; 24 | 25 | /** 26 | * 上传图片 27 | * 28 | * @param file 文件 29 | * @return 文件id 30 | * @throws Exception . 31 | */ 32 | @PostMapping("/image") 33 | @DisableBaseResponse 34 | public String uploadImage(@RequestParam("file") MultipartFile file) throws Exception { 35 | if (file == null || file.isEmpty()) throw new Exception("File cannot be empty"); 36 | return imageService.save(file); 37 | } 38 | 39 | /** 40 | * 获取图片 给前端展示 41 | * 42 | * @param id 文件id 43 | * @param response . 44 | * @throws Exception . 45 | */ 46 | @GetMapping("/image/{id}") 47 | public void getImage(@PathVariable String id, HttpServletResponse response) throws Exception { 48 | Image image = imageService.findById(id); 49 | if (image == null) return; 50 | byte[] data = image.getData(); 51 | response.setContentType("image/jpeg"); 52 | response.setCharacterEncoding("UTF-8"); 53 | OutputStream outputStream = response.getOutputStream(); 54 | InputStream in = new ByteArrayInputStream(data); 55 | int len; 56 | byte[] buf = new byte[1024]; 57 | while ((len = in.read(buf, 0, 1024)) != -1) { 58 | outputStream.write(buf, 0, len); 59 | } 60 | outputStream.close(); 61 | } 62 | 63 | @PostMapping("/video") 64 | @DisableBaseResponse 65 | public String uploadVideo(@RequestParam("file") MultipartFile file) throws IOException { 66 | String id = UUID.randomUUID().toString(); 67 | file.transferTo(new File(savePath + "/" + id + ".mp4")); 68 | return id; 69 | } 70 | 71 | @GetMapping("/video/{id}") 72 | @DisableBaseResponse 73 | public void getVideo(@PathVariable String id, HttpServletResponse response) throws IOException { 74 | String path = savePath + "/" + id + ".mp4"; 75 | FileInputStream fileInputStream = new FileInputStream(path); 76 | byte[] data = new byte[fileInputStream.available()]; 77 | int i = fileInputStream.read(data); 78 | response.setContentType("video/mp4"); 79 | response.setHeader("Content-Disposition", "attachment; filename=" + id + ".mp4"); 80 | response.setContentLength(data.length); 81 | OutputStream os = response.getOutputStream(); 82 | os.write(data); 83 | os.flush(); 84 | os.close(); 85 | fileInputStream.close(); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.model.entity.User; 4 | import com.example.demo.model.vo.UserVo; 5 | import com.example.demo.service.UserService; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import javax.annotation.Resource; 12 | 13 | @RestController 14 | @RequestMapping("/api/user") 15 | public class UserController { 16 | 17 | @Resource 18 | private UserService userService; 19 | 20 | @PostMapping("") 21 | public void save(@RequestBody User user) throws Exception { 22 | userService.save(user); 23 | } 24 | 25 | @PostMapping("/login") 26 | public UserVo login(@RequestBody User user) throws Exception{ 27 | return userService.login(user); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/example/demo/controller/VideoController.java: -------------------------------------------------------------------------------- 1 | package com.example.demo.controller; 2 | 3 | import com.example.demo.mapper.VideoMapper; 4 | import com.example.demo.model.entity.Video; 5 | import com.example.demo.service.VideoService; 6 | import org.springframework.web.bind.annotation.*; 7 | 8 | import javax.annotation.Resource; 9 | import java.util.List; 10 | 11 | @RestController 12 | @RequestMapping("/api/video") 13 | public class VideoController { 14 | 15 | @Resource 16 | private VideoService videoService; 17 | 18 | @GetMapping("") 19 | public List