在线阅读网站|基于Springboot+Vue开发实现小说阅读网站(一)https://developer.aliyun.com/article/1423331
四,核心代码展示
package com.hua.controller; import ch.qos.logback.core.joran.util.beans.BeanUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hua.handler.Result; import com.hua.mapper.TypeMapper; import com.hua.mapper.UserMapper; import com.hua.pojo.*; import com.hua.service.NovelService; import com.hua.service.TypeService; import com.hua.service.UserService; import com.hua.service.impl.AuthorToexamineServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @RestController //@Controller @RequestMapping("/novel") //@ConditionalOnMissingBean(AuthorToexamineServiceImpl.class) //当缺失这个AuthorToexamineServiceImpl的时候,条件成立 ->NovelController ->bean //@Conditional mybatis plus springdatasource public class NovelController { @Resource private NovelService novelService; @Resource private UserService userService; @Autowired private TypeMapper typeMapper; @Autowired private UserMapper userMapper; /** * 条件分页查询Novel * @param pageNum * @param pageSize * @param search * @return */ @GetMapping /** * @RequestParam用于将请求参数区数据映射到功能处理方法的参数上。defaultValue:默认值,表示如果请求中没有同名参数时的默认值 * */ @ResponseBody public Result<?> NovelFindPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String search){ //mybatis-plus中的构造器,创建一个构造器 LambdaQueryWrapper<Novel> wrapper=new LambdaQueryWrapper<Novel>(); //StuUtil工具类内置了很多方法,isNotBlamk判断字符串是否为空 // if (StrUtil.isNotBlank(search)){ //where "novelName" like "123" limit 2,5 1 wrapper.like(StrUtil.isNotBlank(search),Novel::getNovelName,search); // } //TODO Page<NovelVO> resPage = new Page<>(pageNum,pageSize); //limit Page<Novel> page = novelService.page(new Page<>(pageNum, pageSize), wrapper); //DO=>VO BeanUtils.copyProperties(page,resPage,"records"); //TODO 把DO records也给赋值给VO List<Novel> records = page.getRecords(); // records.stream().map() List<NovelVO> list = records.stream().map((item)->{ NovelVO novelVO = new NovelVO(); BeanUtils.copyProperties(item,novelVO); //根据分类id查询分类名称 Type type = typeMapper.selectById(novelVO.getTypeId()); novelVO.setTypeName(type.getTypeName()); User user = userMapper.selectById(novelVO.getUserId()); novelVO.setNickName(user.getNickName()); return novelVO; }).collect(Collectors.toList()); resPage.setRecords(list); resPage.setTotal(page.getTotal()); return Result.success(resPage); } @DeleteMapping //Delete为删除,removeByIds为批量删除 public Result<?> NovelDelete(@RequestBody Set<String> ids){ novelService.removeByIds(ids); return Result.success(); } //新增一条小说的数据 @PostMapping("addNovel") public Result<?> addNovel(@RequestBody Novel novel){ novelService.save(novel); return Result.success(); } //根据用户id查询多行数据 @PostMapping("/selectNovel/{userId}") public Result<?> selectUser(@PathVariable String userId){ LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Novel::getUserId,userId); List<Novel> list = novelService.list(wrapper); return Result.success(list); } //根据用户id查询多行数据,点击量全部相加 @PostMapping("/selectNovelDjl/{userId}") public Result<?> selectNovelDjl(@PathVariable String userId){ LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Novel::getUserId,userId); List<Novel> list = novelService.list(wrapper); Integer zongDjl=0; for (Novel novel : list) { Integer novelHits = novel.getNovelHits(); zongDjl=novelHits+zongDjl; } return Result.success(zongDjl); } //根据分类id查询数据 @PostMapping("/selectImgName/{typeId}") public Result<?> selectImgName(@PathVariable String typeId){ LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Novel::getTypeId,typeId); List<Novel> list = novelService.list(wrapper); return Result.success(list); } //根据小说id查询内容 @PostMapping("/selectTitle/{novelId}") public Result<?> selectNovel(@PathVariable String novelId){ Novel byId = novelService.getById(novelId); LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(User::getUserId,byId.getUserId()); String nickName = userService.getOne(wrapper).getNickName(); byId.setUserId(nickName); return Result.success(byId); } 根据小说id查询内容,(点击量加1) @PostMapping("/selectJiaNovelHits/{novelId}") public Result<?> selectJiaNovelHits(@PathVariable String novelId){ Novel byId = novelService.getById(novelId); LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(User::getUserId,byId.getUserId()); // 设置点击量加1 int novelHits = byId.getNovelHits(); novelHits++; byId.setNovelHits(novelHits); novelService.updateById(byId); String nickName = userService.getOne(wrapper).getNickName(); byId.setUserId(nickName); return Result.success(byId); } //查询出点击量最高的6位 @PostMapping("/selectMaxHits") public Result<?> selectMaxHits(){ LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>(); wrapper.orderByDesc(Novel::getNovelHits); List<Novel> list = novelService.list(wrapper ); return Result.success( list.subList(0,6)); } @PutMapping //put为更新 public Result<?> novelUpdate(@RequestBody Novel novel){ novelService.updateById(novel); return Result.success(novel); } }
package com.hua.controller; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hua.handler.Result; import com.hua.pojo.Novel; import com.hua.service.NovelService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.Set; @RestController @RequestMapping("/novelToexamine") public class NovelToexamineController { @Resource private NovelService novelToexamineService; @GetMapping //Get为查询 public Result<?> novelToexamineFindPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String search){ LambdaQueryWrapper<Novel> wrapper= Wrappers.<Novel>lambdaQuery(); if (StrUtil.isNotBlank(search)){ wrapper.like(Novel::getNovelId,search); } Page<Novel> page = novelToexamineService.page(new Page<>(pageNum, pageSize), wrapper); return Result.success(page); } @PutMapping //put为更新 public Result<?> novelToexamineUpdate(@RequestBody Novel novelToexamine ){ novelToexamineService.updateById(novelToexamine); return Result.success(); } @DeleteMapping //Delete为删除,removeByIds为批量删除 public Result<?>WithdrawalDelete(@RequestBody Set<String> ids){ novelToexamineService.removeByIds(ids); return Result.success(); } }
package com.hua.controller; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.hua.handler.Result; import com.hua.pojo.User; import com.hua.service.UserService; import com.hua.service.WalletService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; import java.util.Set; @RestController @RequestMapping("/user") public class UserController { @Resource private UserService userService; @Resource private WalletService walletService; @PostMapping //post为新增 public Result<?> usersave(@RequestBody User user){ //拿一个User类型的查询条件 where LambdaQueryWrapper<User> usersLambdaQueryWrapper = new LambdaQueryWrapper<>(); //如何查询出数据库中没有该usernam则成功注册,否则用户名重复 User serviceOne = userService.getOne(usersLambdaQueryWrapper.eq(User::getUserName,user.getUserName())); if (serviceOne == null){ userService.save(user); return Result.success(user.getUserId()); } return Result.error("401","用户名存在请重新设置!"); } @PutMapping //put为更新 public Result<?> userUpdate(@RequestBody User user){ userService.updateById(user); return Result.success(user); } @GetMapping //Get为查询 public Result<?> userFindPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String search){ LambdaQueryWrapper<User> wrapper= Wrappers.<User>lambdaQuery(); if (StrUtil.isNotBlank(search)){ wrapper.like(User::getNickName,search); } Page<User> page = userService.page(new Page<>(pageNum, pageSize), wrapper); //您可以使用 getRecords 方法来获取 API 请求中指定的所有用户数据。records里面保存了User表中的所有数据 List<User> records = page.getRecords(); //循环查询 records.forEach(item -> { /** * selectBanlance的作用是查询出wallet表中的walletId和user表中的的walletId, * 传入(user表中的walletId)item.getWalletId() * Long along返回的是wallet表中的余额值 */ Long aLong = walletService.selectBalance(item.getWalletId()); //由于要使用balance(余额)来替换之前表格中的walletId所以要对aLong进行数据类型转换 item.setWalletId(String.valueOf(aLong)); }); return Result.success(page); } @DeleteMapping //Delete为删除,removeByIds为批量删除 public Result<?> userDelete(@RequestBody Set<String> ids){ userService.removeByIds(ids); return Result.success(); } //根据id查询单行数据 @PostMapping("/selectUser/{userId}") public Result<?> selectUser(@PathVariable String userId){ User byId = userService.getById(userId); return Result.success(byId); } //查询用户表中所有数据 @GetMapping("/selectUser") public Result<?> selectUser(){ List<User> list = userService.list(); return Result.success(list); } }
五,项目总结
1.技术要求
在线名著网站系统采用前后端分离的架构思想,极大的简化了系统的复杂结构,同时前端和后端的分离部署,优化了系统的系统,降低前端和后端之间的耦合性,也为后续的二次开发提供良好的开发环境。
通过观察市场类似的系统以及目前流行的技术,同时参考与开源平台上的开源项目,本系统模块设计的编程语言设计采用java作为基础语言,前端基于VUE实现,后端基于SpringBoot的基础上采用SSM框架集实现。
(1)技术实施方案
通过调研,了解在线名著网站系统的内容及其特点,存在和需要解决的主要问题。确定在线名著网站系统的建设目标、意义及功能需求,分析整个系统架构的组成、设计特点、安全特性。对在线名著网站系统的模块进行分析,用UML图给出相应模块的用例图。然后从简到繁一步步完成功能点,首先编写最基础的登陆注册以及退出登录功能,之后完善前端页面。根据前端功能模块一个个完善功能,在实现后台对用户以及名著的管理。以及前后端数据的交互。
(2)系统测试方案
对系统的实验分为三个阶段。第一阶段对单个功能模块进行实验,确认单个功能模块功能完善,功能点可以正常使用。第二阶段对有交互的功能模块进行两两一对的联动实验,确认多个模块之间数据交互、运行正常。第三阶段对整个系统进行细致的实验与测试,在系统搭建完成之后,模拟正常运营的情况下系统是否存在问题与隐患。
2.工作要求
(1)操作可行性:本系统操作简单,设计了良好的用户交换界面与用户引导,操作简洁明了。操作上可行。
(2)技术可行性:本系统使用Java基础语言和成熟的SSM框架集开发,资料文档齐全。并且系统在市场上已经有成熟的同类型系统案例。技术上可行。
(3)经济可行性:本系统需要的是名著的内容资源,名著的初始内容可以引用我国古代的名著内容,这样就不存在内容侵权。以及后期名著内容是用户自主编写就更加不存在搬运侵权的问题了,还可以通过获取点击量来投放广告达到盈利的效果,因此经济上可行。