基于javaweb+mysql的springboot在线小说阅读系统(前后端分离+java+vue+springboot+ssm+mysql+maven)
基于javaweb+mysql的springboot在线小说阅读系统(前后端分离+java+vue+springboot+ssm+mysql+maven)
运行环境
Java≥8、MySQL≥5.7、Node.js≥10
开发工具
后端:eclipse/idea/myeclipse/sts等均可配置运行
前端:WebStorm/VSCode/HBuilderX等均可
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot在线小说阅读系统(前后端分离+java+vue+springboot+ssm+mysql+maven)
一、项目简述
本系统功能包括: 普通用户端登录注册,小说的分类,日榜,月榜,年榜, 小说的阅读,分章节,小说的评论,收藏,推荐等等,以 及后台小说的维护,上架,编辑等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX (Webstorm也 行)+ Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支 持)。
项目技术: Springboot + Maven + Mybatis + Vue , B/S 模式+ Maven等等
@GetMapping("/count")
@ApiOperation("查询Recommend")
public ResponseObject getCount(Integer novel_id) {
log.info("查询Recommend");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else {
return new ResponseObject("200", "操作成功", recommendService.selectByNovel_id(novel_id).size());
}
}
@GetMapping("/novel")
@ApiOperation("查询Recommend的Novel")
public ResponseObject getNovel(Integer user_id) {
log.info("查询Recommend的Novel");
if (user_id == null) {
throw new ControllerException("user_id不可为null");
} else {
return new ResponseObject("200", "操作成功", novelService.selectByUser_idOfRecommend(user_id));
}
}
}
}
@RestController
@RequestMapping("/api")
@Api(tags = "共同前缀:/api", description = "AppController")
@Slf4j
public class AppController {
@Autowired
UserService userService;
@Autowired
NovelService novelService;
@GetMapping("/me")
@ApiOperation("查询Me")
@PreAuthorize("isAuthenticated()")
public ResponseObject getMe() {
log.info("查询Me");
return new ResponseObject("200", "操作成功", userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal()));
}
}
novelService.selectByRank(MyUtils.simpleDateFormat.format(date), limit_count));
}
}
@PostMapping("/image")
@ApiOperation("上传image")
@PreAuthorize("isAuthenticated()")
public ResponseObject postImage(MultipartFile file) throws Exception {
log.info("上传image");
if (file == null) {
throw new ControllerException("上传的文件不可为null");
} else {
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (!suffix.equals(".png")) {
throw new RuntimeException("上传的文件必须为png文件");
} else {
String directoryPath = ResourceUtils.getURL("src").getPath() + "main/resources/static/images/";
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
String newFileName = new Date().getTime() + ".png";
File file2 = new File(directory, newFileName);
if (!file2.exists()) {
file2.createNewFile();
}
// 保存文件
file.transferTo(file2);
return new ResponseObject("200", "操作成功", newFileName);
}
}
}
@PatchMapping("/image")
@ApiOperation("修改image")
@PreAuthorize("isAuthenticated()")
public ResponseObject patchImage(@RequestBody HashMap<String, String> data) {
log.info("修改image");
String image = data.get("image");
String novel_idString = data.get("novel_id");
if (image == null || image.equals("")) {
throw new ControllerException("image不可为null,也不可为空字符串");
} else if (novel_idString == null || novel_idString.equals("")) {
throw new ControllerException("novel_id不可为null");
} else {
Integer novel_id = Integer.parseInt(novel_idString);
Novel novel = novelService.selectById(novel_id);
@PostMapping
@ApiOperation("新增Chapter")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody HashMap<String, String> data) {
log.info("新增Chapter");
if (data.get("title") == null || data.get("title").equals("")) {
throw new ControllerException("volume_id不可为null");
} else if (data.get("volume_id") == null || data.get("volume_id").equals("")) {
throw new ControllerException("title不可为null,也不可为空字符串");
} else if (data.get("chapterContent") == null || data.get("chapterContent").equals("")) {
throw new ControllerException("chapterContent不可为null,也不可为空字符串");
} else {
try {
// 文件夹
File directory = new File(ResourceUtils.getURL("src").getPath() + "main/resources/static/txt/");
if (!directory.exists()) {
directory.mkdirs();
}
// 文件
String content = new Date().getTime() + ".txt";
File file2 = new File(directory, content);
if (!file2.exists()) {
file2.createNewFile();
}
// 往文件内写内容
FileWriter fileWriter = new FileWriter(file2);
fileWriter.write(data.get("chapterContent"));
fileWriter.flush();
fileWriter.close();
Chapter chapter = new Chapter();
chapter.setTitle(data.get("title"));
chapter.setVolume_id(Integer.parseInt(data.get("volume_id")));
chapter.setContent(content);
return new ResponseObject("200", "操作成功", chapterService.insert(chapter));
} catch (Exception e) {
throw new ControllerException("操作失败");
}
}
}
@GetMapping
@ApiOperation("查询Chapter")
public ResponseObject get(Integer volume_id) {
log.info("查询Chapter");
if (volume_id == null) {
@RestController
@RequestMapping("/api/comment")
@Api(tags = "共同前缀:/api/comment", description = "CommentController")
@Slf4j
public class CommentController {
@Autowired
CommentService commentService;
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增Comment")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Comment comment) {
log.info("新增Comment");
if (comment.getId() != null) {
throw new ControllerException("id必须为null");
} else if (comment.getNovel_id() == null) {
throw new ControllerException("novel_id不可为null");
} else if (comment.getContent() == null || comment.getContent().equals("")) {
throw new ControllerException("content不可为null,也不可为空字符串");
} else {
comment.setUser_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
return new ResponseObject("200", "操作成功", commentService.insert(comment));
}
}
@GetMapping
@ApiOperation("查询Comment")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Comment");
if (novel_id != null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_idNovel_id(user_id, novel_id));
} else if (novel_id != null && user_id == null) {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id));
} else if (novel_id == null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_id(user_id));
} else {
throw new ControllerException("novel_id和user_id不可同时为null");
}
}
@GetMapping("/count")
@ApiOperation("查询Comment")
@DeleteMapping
@ApiOperation("删除Recommend")
@PreAuthorize("isAuthenticated()")
public ResponseObject delete(Integer novel_id, Integer user_id) {
log.info("删除Recommend");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else if (user_id == null) {
throw new ControllerException("user_id不可为null");
} else {
Recommend recommend = recommendService.selectByUser_idNovel_id(user_id, novel_id);
if (recommend == null) {
throw new ControllerException("该用户还未推荐该小说,无法取消推荐");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == recommend.getUser_id() || user.getRole().equals("ADMIN")) {
recommendService.deleteById(recommend.getId());
return new ResponseObject("200", "操作成功", null);
} else {
throw new ControllerException("该用户无权限取消推荐");
}
}
}
}
@GetMapping("/count")
@ApiOperation("查询Recommend")
public ResponseObject getCount(Integer novel_id) {
log.info("查询Recommend");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else {
return new ResponseObject("200", "操作成功", recommendService.selectByNovel_id(novel_id).size());
}
}
@GetMapping("/novel")
@ApiOperation("查询Recommend的Novel")
public ResponseObject getNovel(Integer user_id) {
log.info("查询Recommend的Novel");
if (user_id == null) {
throw new ControllerException("user_id不可为null");
} else {
return new ResponseObject("200", "操作成功", novelService.selectByUser_idOfRecommend(user_id));
}
}
}
}
}
@DeleteMapping
@ApiOperation("删除Follow")
@PreAuthorize("isAuthenticated()")
public ResponseObject delete(Integer following_id) {
log.info("删除Follow");
if (following_id == null) {
throw new ControllerException("following_id不可为null");
} else {
Integer follower_id = userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId();
Follow follow = followService.selectByFollower_idFollowing_id(follower_id, following_id);
if (follow == null) {
throw new ControllerException("该用户未关注,无法取消关注");
} else {
followService.deleteById(follow.getId());
return new ResponseObject("200", "操作成功", null);
}
}
}
}
@RestController
@RequestMapping("/api/category")
@Api(tags = "共同前缀:/api/category", description = "CategoryController")
@Slf4j
public class CategoryController {
} else if (follower_id != null && following_id == null) {
return new ResponseObject("200", "操作成功", userService.selectByFollower_id(follower_id));
} else if (follower_id == null && following_id != null) {
return new ResponseObject("200", "操作成功", userService.selectByFollowing_id(following_id));
} else {
throw new ControllerException("follower_id与following_id不可同时为null");
}
}
@DeleteMapping
@ApiOperation("删除Follow")
@PreAuthorize("isAuthenticated()")
public ResponseObject delete(Integer following_id) {
log.info("删除Follow");
if (following_id == null) {
throw new ControllerException("following_id不可为null");
} else {
Integer follower_id = userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId();
Follow follow = followService.selectByFollower_idFollowing_id(follower_id, following_id);
if (follow == null) {
throw new ControllerException("该用户未关注,无法取消关注");
} else {
followService.deleteById(follow.getId());
return new ResponseObject("200", "操作成功", null);
}
}
}
}
@Api(tags = "共同前缀:/api/recommend", description = "RecommendController")
@Slf4j
public class RecommendController {
@Autowired
RecommendService recommendService;
@Autowired
UserService userService;
@Autowired
NovelService novelService;
@PostMapping
@ApiOperation("新增Recommend")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Recommend recommend) {
log.info("新增Recommend");
if (recommend.getId() != null) {
throw new ControllerException("id必须为null");
} else if (recommend.getNovel_id() == null) {
throw new ControllerException("novel_id不可为null");
} else {
recommend.setUser_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
if (recommendService.selectByUser_idNovel_id(recommend.getUser_id(), recommend.getNovel_id()) != null) {
throw new ControllerException("该用户已经推荐过该小说了,不可重复推荐");
} else {
return new ResponseObject("200", "操作成功", recommendService.insert(recommend));
}
}
}
@GetMapping
@ApiOperation("查询Recommend")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Recommend");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else if (user_id == null) {
throw new ControllerException("user_id不可为null");
} else {
@RestController
@RequestMapping("/api/novel")
@Api(tags = "共同前缀:/api/novel", description = "NovelController")
@Slf4j
public class NovelController {
@Autowired
NovelService novelService;
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增Novel")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Novel novel) {
log.info("新增Novel");
if (novel.getId() != null) {
throw new ControllerException("id必须为null");
} else if (novel.getUser_id() == null) {
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询Novel")
public ResponseObject getById(@PathVariable Integer id) {
log.info("查询Novel");
if (id != null) {
return new ResponseObject("200", "操作成功", novelService.selectById(id));
} else {
throw new ControllerException("id不可为null");
}
}
@PatchMapping("/{id:[0-9]+}")
@ApiOperation("修改Novel")
@PreAuthorize("isAuthenticated()")
public ResponseObject patchById(@PathVariable Integer id, @RequestBody Novel novel) {
log.info("修改Novel");
if (id == null) {
throw new ControllerException("id不可为null");
} else {
Novel novel2 = novelService.selectById(id);
if (novel2 == null) {
throw new ControllerException("不存在为该id的novel");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novel2.getUser_id() || user.getRole().equals("ADMIN")) {
if (novel.getName() != null && !novel.getName().equals("")) {
novel2.setName(novel.getName());
} else if (novel.getSummary() != null && !novel.getSummary().equals("")) {
novel2.setSummary(novel.getSummary());
} else if (novel.getCategory_id() != null) {
novel2.setCategory_id(novel.getCategory_id());
} else if (novel.getMultiplier() != null) {
novel2.setMultiplier(novel.getMultiplier());
} else if (novel.getAddend() != null) {
novel2.setAddend(novel.getAddend());
} else {
throw new ControllerException("请传入需要修改的数据,如name,summary,category_id,multiplier,addend");
}
return new ResponseObject("200", "操作成功", novelService.update(novel2));
} else {
throw new ControllerException("该用户无权限修改该小说");
}
}
}
}
@GetMapping("/rank")
@ApiOperation("查询Novel")
public ResponseObject Rank(Integer day_count, Integer limit_count) {
if (day_count == null) {
throw new ControllerException("day_count不可为null");
} else if (limit_count == null) {
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Comment comment) {
log.info("新增Comment");
if (comment.getId() != null) {
throw new ControllerException("id必须为null");
} else if (comment.getNovel_id() == null) {
throw new ControllerException("novel_id不可为null");
} else if (comment.getContent() == null || comment.getContent().equals("")) {
throw new ControllerException("content不可为null,也不可为空字符串");
} else {
comment.setUser_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
return new ResponseObject("200", "操作成功", commentService.insert(comment));
}
}
@GetMapping
@ApiOperation("查询Comment")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Comment");
if (novel_id != null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_idNovel_id(user_id, novel_id));
} else if (novel_id != null && user_id == null) {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id));
} else if (novel_id == null && user_id != null) {
return new ResponseObject("200", "操作成功", commentService.selectByUser_id(user_id));
} else {
throw new ControllerException("novel_id和user_id不可同时为null");
}
}
@GetMapping("/count")
@ApiOperation("查询Comment")
public ResponseObject getCount(Integer novel_id) {
log.info("查询Comment");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else {
return new ResponseObject("200", "操作成功", commentService.selectByNovel_id(novel_id).size());
@RestController
@RequestMapping("/api/user")
@Api(tags = "共同前缀:/api/user", description = "UserController")
@Slf4j
public class UserController {
@Autowired
UserService userService;
@PostMapping
@ApiOperation("新增User")
public ResponseObject post(String username, String password) {
log.info("新增User");
if (username == null || username.equals("")) {
throw new ControllerException("username不可为null,也不可为空字符串");
} else if (password == null || password.equals("")) {
throw new ControllerException("password不可为null,也不可为空字符串");
} else if (userService.selectByUsername(username) != null) {
throw new ControllerException("该username已被使用");
} else {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setRole("VIP1");
user.setNickname(new Date().getTime() + "");
user.setImage("default_user_image.png");
user.setEmail("该用户没有填写邮箱");
user.setPhone("该用户没有填写手机号码");
user.setProfile("该用户没有填写个人简介");
return new ResponseObject("200", "操作成功", userService.insert(user));
}
}
@GetMapping("/{id:[0-9]+}")
@ApiOperation("查询User")
} else {
throw new ControllerException("请传入需要修改的数据,如name,summary,category_id,multiplier,addend");
}
return new ResponseObject("200", "操作成功", novelService.update(novel2));
} else {
throw new ControllerException("该用户无权限修改该小说");
}
}
}
}
@GetMapping("/rank")
@ApiOperation("查询Novel")
public ResponseObject Rank(Integer day_count, Integer limit_count) {
if (day_count == null) {
throw new ControllerException("day_count不可为null");
} else if (limit_count == null) {
throw new ControllerException("limit_count不可为null");
} else {
Date date = new Date(new Date().getTime() - (long) 1000 * 60 * 60 * 24 * day_count);
return new ResponseObject("200", "操作成功",
novelService.selectByRank(MyUtils.simpleDateFormat.format(date), limit_count));
}
}
@PostMapping("/image")
@ApiOperation("上传image")
@PreAuthorize("isAuthenticated()")
public ResponseObject postImage(MultipartFile file) throws Exception {
log.info("上传image");
if (file == null) {
throw new ControllerException("上传的文件不可为null");
} else {
String fileName = file.getOriginalFilename();
String suffix = fileName.substring(fileName.lastIndexOf("."));
if (!suffix.equals(".png")) {
throw new RuntimeException("上传的文件必须为png文件");
} else {
String directoryPath = ResourceUtils.getURL("src").getPath() + "main/resources/static/images/";
File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdirs();
}
String newFileName = new Date().getTime() + ".png";
File file2 = new File(directory, newFileName);
if (!file2.exists()) {
file2.createNewFile();
}
// 保存文件
file.transferTo(file2);
return new ResponseObject("200", "操作成功", newFileName);
}
}
}
UserService userService;
@Autowired
NovelService novelService;
@PostMapping
@ApiOperation("新增Recommend")
@PreAuthorize("isAuthenticated()")
public ResponseObject post(@RequestBody Recommend recommend) {
log.info("新增Recommend");
if (recommend.getId() != null) {
throw new ControllerException("id必须为null");
} else if (recommend.getNovel_id() == null) {
throw new ControllerException("novel_id不可为null");
} else {
recommend.setUser_id(userService
.selectByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal())
.getId());
if (recommendService.selectByUser_idNovel_id(recommend.getUser_id(), recommend.getNovel_id()) != null) {
throw new ControllerException("该用户已经推荐过该小说了,不可重复推荐");
} else {
return new ResponseObject("200", "操作成功", recommendService.insert(recommend));
}
}
}
@GetMapping
@ApiOperation("查询Recommend")
public ResponseObject get(Integer novel_id, Integer user_id) {
log.info("查询Recommend");
if (novel_id == null) {
throw new ControllerException("novel_id不可为null");
} else if (user_id == null) {
throw new ControllerException("user_id不可为null");
} else {
return new ResponseObject("200", "操作成功", recommendService.selectByUser_idNovel_id(user_id, novel_id));
}
}
@DeleteMapping
@ApiOperation("删除Recommend")
@PreAuthorize("isAuthenticated()")
public ResponseObject delete(Integer novel_id, Integer user_id) {
Novel novel = novelService.selectById(novel_id);
if (novel == null) {
throw new ControllerException("根据novel_id查询出来的novel为null");
} else {
User user = userService.selectByUsername(
(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if (user.getId() == novel.getUser_id() || user.getRole().equals("ADMIN")) {
novel.setImage(image);
return new ResponseObject("200", "操作成功", novelService.update(novel));
} else {
throw new ControllerException("该用户无权限修改小说头像");
}
}
}
}
}
@RestController
@RequestMapping("/api/chapter")
@Api(tags = "共同前缀:/api/chapter", description = "ChapterController")
@Slf4j
public class ChapterController {
//无权限异常处理器
@Component
@Slf4j
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Autowired
ObjectMapper objectMapper;
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.info("已认证无权限,返回JSON格式的异常信息");
response.setStatus(403);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(new ResponseObject("403", "已认证无权限", null)));
writer.flush();
writer.close();
}
}