基于javaweb+mysql的springboot在线学习系统(java+springboot+mybatis+vue+mysql+redis)
运行环境
Java≥8、MySQL≥5.7、Node.js≥10
开发工具
后端:eclipse/idea/myeclipse/sts等均可配置运行
前端:WebStorm/VSCode/HBuilderX等均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot在线学习系统(java+springboot+mybatis+vue+mysql+redis)
一、项目运行 环境配置:
Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
项目技术:
Spring + SpringBoot+ mybatis + Maven + Vue 等等组成,B/S模式 + Maven管理等等。
* 修改用户它信息(除了密码和头像)
* 其实也就只修改3个字段,邮箱、手机、性别
*/
@PostMapping("/updateProfile")
public String updateProfile(@RequestBody User user) {
boolean isOk = userService.updateProfile(user, hostHolder.getUser().getId());
int code = isOk ? 200 : 2100;
String msg = isOk ? "修改成功!" : "修改失败!";
return SystemUtil.getJSONString(code, msg);
}
}
/**
*/
@RestController
@RequestMapping("/note")
public class NoteController {
private static final Logger logger = LoggerFactory.getLogger(NoteController.class);
@Value("${onlinelearning.path.upload.file}")
private String fileUploadPath;
@Value("${onlinelearning.path.domain}")
private String domain;
//日志切面
@Slf4j
@Aspect
@Component
public class SysLogAspect {
@Pointcut("execution(* com.gll.onlinelearning.controller..*.*(..))")
public void log() {
}
@Before("log()")
public void doBefore(JoinPoint joinPoint) throws UnsupportedEncodingException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String method = request.getMethod();
StringBuffer paramsValue = new StringBuffer();
Object paramsName = null;
// get请求
if (HttpMethod.GET.toString().equals(method)) {
String queryString = request.getQueryString();
if (!StringUtils.isEmpty(queryString)) {
paramsName = JSON.parseObject(JSON.toJSONString(joinPoint.getSignature())).get("parameterNames");
paramsValue.append(URLDecoder.decode(queryString, "UTF-8"));
}
} else {
//其他请求
Object[] paramsArray = joinPoint.getArgs();
paramsName = JSON.parseObject(JSON.toJSONString(joinPoint.getSignature())).get("parameterNames");
for (Object o : paramsArray) {
paramsValue.append(o).append(" ");
}
}
String ip = HttpIpUtils.getClientIp(request);
log.info("URLParamName:" + paramsName);
log.info("URLParamValue:" + paramsValue);
log.info("URL:{},HTTP_METHOD:{},IP:{},Method:{}", request.getRequestURL().toString(), request.getMethod(), ip, joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
}
}
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if ("127.0.0.1".equals(ip)) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
assert inet != null;
ip = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
// / "***.***.***.***".length()
if (ip != null && ip.length() > 15) {
// = 15
if (ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
}
} catch (Exception e) {
ip = "";
}
return ip;
}
}
@Slf4j
/**
* 原始异常处理
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
String handleExceptionForErrorOne(Exception e, HttpServletRequest request) {
logger.info("Exception message:{}", e.getMessage());
logger.info("Exception from:{}", e.getCause());
logger.info("Exception print:{}", e);
return SystemUtil.getJSONString(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
/**
* 自定义全局异常处理
*/
@ExceptionHandler(GlobalException.class)
String handleExceptionForErrorTwo(GlobalException e, HttpServletRequest request) {
logger.info("MyException message:{}", e.getMessage());
logger.info("MyException from:{}", e.getCause());
logger.info("MyException print:{}", e);
return SystemUtil.getJSONString(e.getCode(), e.getMessage());
}
}
/**
*/
@RestController
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");
if (!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
return request.getRemoteAddr();
}
/**
* 获取用户真实IP地址,不使用request.getRemoteAddr(); 的原因是有可能用户使用了代理软件方式避免真实IP地址
*/
public static String getRealClientIp(HttpServletRequest request) {
String ip;
try {
ip = request.getHeader("x-forwarded-for");
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if ("127.0.0.1".equals(ip)) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
/**
*/
@RestController
@RequestMapping("/comment")
public class CommentController {
@Resource
private CommentService commentService;
@Resource
private PostService postService;
/**
* 添加评论
*/
@PostMapping("/add")
public String addComment(@RequestBody Comment comment) {
boolean isOk = commentService.save(comment);
//重新查询一下插入的评论信息
Comment one = isOk ? commentService.getById(comment.getId()) : null;
if (isOk) {
//查询成功才往 post 表更新 replynum 字段
postService.updateReplynumOfPost(1, comment.getPostId());
return SystemUtil.getJSONString(200, "评论成功!", new HashMap<String, Object>() {{
put("comment", one);
}});
}
return SystemUtil.getJSONString(4000, "评论失败!");
}
/**
* todo:暂时不做这个功能
* 传评论 id 和 评论层级 level
* 删除评论,传一个参数,评论id,还要对其子评论进行递归删除
* 分情况讨论
String msg = isOk ? "发表成功!" : "发表失败!";
return SystemUtil.getJSONString(code, msg);
}
/**
* 分页获取帖子列表,按照更新时间倒叙排,只需要传页码,搜索字段非必须
*/
@GetMapping("/all")
public String getPageOfPost(Integer pageIndex, String searchContent) {
List<PageOfPostResultVO> list = postService.getPageOfPost(pageIndex, searchContent);
int total = postService.getCountOfPostByContent(searchContent);
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("total", total);
put("postList", list);
}});
}
/**
* 获取某条帖子的信息,只需要传帖子 id
*/
@GetMapping("/get/{id}")
public String getPostById(@PathVariable("id") Integer id) {
Post post = postService.getById(id);
//再查出当前帖子的用户信息一起传给前端
User user = userService.getUserById(post.getUid());
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("pInfo", post);
put("uInfo", SystemUtil.handleUser(user));
}});
}
/**
* 更新某条帖子信息,传帖子id,content,pictures 这3个字段即可
*/
@PostMapping("/update")
public String updatePost(@RequestBody Post post) {
boolean isOk = postService.updatePost(post);
int code = isOk ? 200 : 2300;
String msg = isOk ? "更新成功!" : "更新失败!";
return SystemUtil.getJSONString(code, msg);
}
/**
* 删除帖子,当前用户为登录的用户
*/
@DeleteMapping("/del/{id}")
public String delPost(@PathVariable("id") Integer id) {
boolean isOk = postService.delPostById(id);
int code = isOk ? 200 : 2300;
String msg = isOk ? "删除帖子成功!" : "删除帖子失败!";
return SystemUtil.getJSONString(code, msg);
}
List<PageOfPostResultVO> list = postService.getPageOfPost(pageIndex, searchContent);
int total = postService.getCountOfPostByContent(searchContent);
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("total", total);
put("postList", list);
}});
}
/**
* 获取某条帖子的信息,只需要传帖子 id
*/
@GetMapping("/get/{id}")
public String getPostById(@PathVariable("id") Integer id) {
Post post = postService.getById(id);
//再查出当前帖子的用户信息一起传给前端
User user = userService.getUserById(post.getUid());
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("pInfo", post);
put("uInfo", SystemUtil.handleUser(user));
}});
}
/**
* 更新某条帖子信息,传帖子id,content,pictures 这3个字段即可
*/
@PostMapping("/update")
public String updatePost(@RequestBody Post post) {
boolean isOk = postService.updatePost(post);
int code = isOk ? 200 : 2300;
String msg = isOk ? "更新成功!" : "更新失败!";
return SystemUtil.getJSONString(code, msg);
}
/**
* 删除帖子,当前用户为登录的用户
*/
@DeleteMapping("/del/{id}")
public String delPost(@PathVariable("id") Integer id) {
boolean isOk = postService.delPostById(id);
int code = isOk ? 200 : 2300;
String msg = isOk ? "删除帖子成功!" : "删除帖子失败!";
return SystemUtil.getJSONString(code, msg);
}
}
*/
@RestController
@RequestMapping("/note")
public class NoteController {
private static final Logger logger = LoggerFactory.getLogger(NoteController.class);
@Value("${onlinelearning.path.upload.file}")
private String fileUploadPath;
@Value("${onlinelearning.path.domain}")
private String domain;
@Resource
private HostHolder hostHolder;
@Resource
private NoteService noteService;
/**
* 分页查询出所有用户上传的笔记
*/
@GetMapping("/all/{pageIndex}")
public String getPageOfNote(@PathVariable("pageIndex") Integer pageIndex) {
IPage<Note> list = noteService.getPageOfNote(pageIndex);
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("total", list.getTotal());
put("noteList", list.getRecords());
}});
}
/**
* 上传笔记
*/
@PostMapping("/add")
public String uploadHeader(MultipartFile file) {
String fileName;
if (file == null || (fileName = file.getOriginalFilename()) == null) {
return SystemUtil.getJSONString(2013, "您还没有选择文件!");
}
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (StringUtils.isBlank(suffix)) {
return SystemUtil.getJSONString(2013, "文件格式不正确!");
}
// 生成随机文件名
String newFileName = SystemUtil.generateUUID() + suffix;
// 构建上传文件的存放 "文件夹" 路径
File fileDir = new File(fileUploadPath);
private HostHolder hostHolder;
@Resource
private UserService userService;
/**
* 更新头像
*/
@PostMapping("/updateAvatar")
public String uploadHeader(MultipartFile file) {
String fileName;
if (file == null || (fileName = file.getOriginalFilename()) == null) {
return SystemUtil.getJSONString(2013, "您还没有选择图片!");
}
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (StringUtils.isBlank(suffix) || !imgExtends.contains(suffix.toLowerCase())) {
return SystemUtil.getJSONString(2013, "图片格式不正确!");
}
// 生成随机文件名
String newFileName = SystemUtil.generateUUID() + suffix;
// 构建上传文件的存放 "文件夹" 路径
File fileDir = new File(imgUploadPath);
if (!fileDir.exists()) {
// 递归生成文件夹
fileDir.mkdirs();
}
// 确定文件存放的路径
File dest = new File(fileDir.getAbsolutePath() + "/" + newFileName);
try {
// 存储文件
file.transferTo(dest);
} catch (IOException e) {
logger.error("上传图片失败: " + e.getMessage());
throw new RuntimeException("上传文件失败,服务器发生异常!", e);
}
// 更新当前用户的头像的路径(web访问路径)
// http://localhost:8006/user/header/xxx.png
User user = hostHolder.getUser();
String headerUrl = domain + "/user/header/" + newFileName;
userService.updateHeader(user.getId(), headerUrl);
return SystemUtil.getJSONString(200, "上传成功!", new HashMap<String, Object>() {{
put("imgUrl", headerUrl);
}});
}
/**
* 获取用户头像
*/
@GetMapping("/header/{fileName}")
public void getUserHeader(@PathVariable("fileName") String fileName, HttpServletResponse response) {
// 服务器存放路径
fileName = imgUploadPath + "/" + fileName;
// 文件后缀
/**
* 真实ip地址
*/
public static String getClientIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");
if (!StringUtils.isEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
return request.getRemoteAddr();
}
/**
* 获取用户真实IP地址,不使用request.getRemoteAddr(); 的原因是有可能用户使用了代理软件方式避免真实IP地址
*/
public static String getRealClientIp(HttpServletRequest request) {
String ip;
try {
ip = request.getHeader("x-forwarded-for");
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if ((ip == null) || (ip.length() == 0) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
//日志切面
@Slf4j
@Aspect
@Component
public class SysLogAspect {
@Pointcut("execution(* com.gll.onlinelearning.controller..*.*(..))")
public void log() {
}
@Before("log()")
public void doBefore(JoinPoint joinPoint) throws UnsupportedEncodingException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String method = request.getMethod();
StringBuffer paramsValue = new StringBuffer();
Object paramsName = null;
// get请求
if (HttpMethod.GET.toString().equals(method)) {
String queryString = request.getQueryString();
if (!StringUtils.isEmpty(queryString)) {
paramsName = JSON.parseObject(JSON.toJSONString(joinPoint.getSignature())).get("parameterNames");
paramsValue.append(URLDecoder.decode(queryString, "UTF-8"));
}
} else {
//其他请求
Object[] paramsArray = joinPoint.getArgs();
paramsName = JSON.parseObject(JSON.toJSONString(joinPoint.getSignature())).get("parameterNames");
for (Object o : paramsArray) {
paramsValue.append(o).append(" ");
}
}
String ip = HttpIpUtils.getClientIp(request);
log.info("URLParamName:" + paramsName);
log.info("URLParamValue:" + paramsValue);
log.info("URL:{},HTTP_METHOD:{},IP:{},Method:{}", request.getRequestURL().toString(), request.getMethod(), ip, joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
}
}
public class CommonAdvice {
private static Logger logger = LoggerFactory.getLogger(CommonAdvice.class);
/**
* 原始异常处理
*/
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
String handleExceptionForErrorOne(Exception e, HttpServletRequest request) {
logger.info("Exception message:{}", e.getMessage());
logger.info("Exception from:{}", e.getCause());
logger.info("Exception print:{}", e);
return SystemUtil.getJSONString(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
/**
* 自定义全局异常处理
*/
@ExceptionHandler(GlobalException.class)
String handleExceptionForErrorTwo(GlobalException e, HttpServletRequest request) {
logger.info("MyException message:{}", e.getMessage());
logger.info("MyException from:{}", e.getCause());
logger.info("MyException print:{}", e);
return SystemUtil.getJSONString(e.getCode(), e.getMessage());
}
}
/**
*/
@RestController
@RequestMapping("/subject")
public class SubjectController {
@Resource
/**
*/
@RestController
@RequestMapping("/note")
public class NoteController {
private static final Logger logger = LoggerFactory.getLogger(NoteController.class);
@Value("${onlinelearning.path.upload.file}")
private String fileUploadPath;
@Value("${onlinelearning.path.domain}")
private String domain;
@Resource
private HostHolder hostHolder;
@Resource
private NoteService noteService;
/**
* 分页查询出所有用户上传的笔记
*/
@GetMapping("/all/{pageIndex}")
public String getPageOfNote(@PathVariable("pageIndex") Integer pageIndex) {
IPage<Note> list = noteService.getPageOfNote(pageIndex);
return SystemUtil.getJSONString(200, "", new HashMap<String, Object>() {{
put("total", list.getTotal());
put("noteList", list.getRecords());
}});
}
/**
/**
*/
@RestController
@RequestMapping("/post")
public class PostController {
private static final Logger logger = LoggerFactory.getLogger(PostController.class);
private final static List<String> imgExtends = Arrays.asList(".jpg", ".png", ".jpeg", ".gif");
@Value("${onlinelearning.path.upload.img}")
private String imgUploadPath;
@Value("${onlinelearning.path.domain}")
private String domain;
@Resource
private PostService postService;
@Resource
private HostHolder hostHolder;
@Resource
private UserService userService;
/**
* 上传图片
}
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (StringUtils.isBlank(suffix)) {
return SystemUtil.getJSONString(2013, "文件格式不正确!");
}
// 生成随机文件名
String newFileName = SystemUtil.generateUUID() + suffix;
// 构建上传文件的存放 "文件夹" 路径
File fileDir = new File(fileUploadPath);
if (!fileDir.exists()) {
// 递归生成文件夹
fileDir.mkdirs();
}
// 确定文件存放的路径
File dest = new File(fileDir.getAbsolutePath() + "/" + newFileName);
try {
// 存储文件
file.transferTo(dest);
} catch (IOException e) {
logger.error("上传文件失败: " + e.getMessage());
throw new RuntimeException("上传文件失败,服务器发生异常!", e);
}
// 笔记下载路径
// http://localhost:8006/note/download/xxxxx.pdf
User user = hostHolder.getUser();
String downloadUrl = domain + "/note/download/" + newFileName;
Note note = new Note();
note.setUid(user.getId());
note.setUsername(user.getUsername());
note.setFilename(fileName);
note.setDownloadUrl(downloadUrl);
noteService.save(note);
return SystemUtil.getJSONString(200, "上传成功!");
}
/**
* 下载笔记
*/
@GetMapping("/download/{fileId}")
public void downloadNote(@PathVariable("fileId") String fileId, String filename,
HttpServletResponse resp) {
String filePath = fileUploadPath + "/" + fileId;
try (
// java 7 写在这里会自动添加finally代码块来关闭流
FileInputStream fis = new FileInputStream(filePath);
OutputStream os = resp.getOutputStream()
) {
// 服务器存放路径
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/octet-stream"); //告诉浏览器输出内容为流
private String domain;
@Resource
private PostService postService;
@Resource
private HostHolder hostHolder;
@Resource
private UserService userService;
/**
* 上传图片
*/
@PostMapping("/imgUpload")
public String uploadHeader(MultipartFile file) {
String fileName;
if (file == null || (fileName = file.getOriginalFilename()) == null) {
return SystemUtil.getJSONString(2013, "您还没有选择图片!");
}
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (StringUtils.isBlank(suffix) || !imgExtends.contains(suffix.toLowerCase())) {
return SystemUtil.getJSONString(2013, "图片格式不正确!");
}
// 生成随机文件名
String newFileName = SystemUtil.generateUUID() + suffix;
// 构建上传文件的存放 "文件夹" 路径
File fileDir = new File(imgUploadPath);
if (!fileDir.exists()) {
// 递归生成文件夹
fileDir.mkdirs();
}
// 确定文件存放的路径
File dest = new File(fileDir.getAbsolutePath() + "/" + newFileName);
try {
// 存储文件
file.transferTo(dest);
} catch (IOException e) {
logger.error("上传图片失败: " + e.getMessage());
throw new RuntimeException("上传图片失败,服务器发生异常!", e);