文件上传下载
文件下载介绍
文件上传代码实现
服务端上传:
@RestController @RequestMapping("/common") @Slf4j public class CommonController { @Value("${reggie.path}") private String basePath;//从配置文件读取设置好的basePath @PostMapping("/upload") public R upload(MultipartFile file){ //file是一个临时文件,需要转存到地址中,否则请求结束就会自动删除 //原始文件名 String originalFilename = file.getOriginalFilename(); //截取文件类型的字符串.jpg/.jpeg等等 String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); //使用UUID重新生成文件名 防止文件重名导致覆盖 String fileName = UUID.randomUUID().toString() + substring; //创建文件路径,并检查文件夹是否存在 File dir =new File(basePath); //如果不存在就新建文件夹 if (!dir.exists()){ dir.mkdirs(); } try { //将文件转存到我们指定的文件夹里 file.transferTo(new File(basePath+fileName)); } catch (IOException e) { throw new RuntimeException(e); } //将fileName返回,后续还会用到 return new R(1,fileName); } }
文件下载实现
@GetMapping("/download") public void download(String name, HttpServletResponse response){ //输入流 读取文件内容 try { FileInputStream fileInputStream =new FileInputStream(new File(basePath+name)); //输出流,通过输出流将文件写会浏览器,在浏览器展示图片 ServletOutputStream outputStream=response.getOutputStream(); int len = 0; byte[] bytes=new byte[1024]; while ((len=fileInputStream.read(bytes))!=-1){ outputStream.write(bytes,0,len); outputStream.flush(); } //关闭资源 outputStream.close(); fileInputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } }
新增菜品
准备工作:
梳理交互过程
下拉框数据:
//根据条件查询分类数据 @GetMapping("/list") public R list(Category category){ LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>(); //添加条件 queryWrapper.eq(category.getType()!=null,Category::getType,category.getType()); //添加排序条件 queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime); List<Category> list = categoryService.list(queryWrapper); return new R(1,"查询成功",list); }
DishServletImpl:
Service public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService { @Autowired private DishFlavorService dishFlavorService; /** * 新增菜品,同时保存对应的口味数据 * @param dishDto */ @Override @Transactional public void saveWithFlavor(DishDto dishDto) { //保存菜品的基本信息到菜品表dish this.save(dishDto); Long dishId = dishDto.getId();//菜品Id //菜品口味 List<DishFlavor> flavors =dishDto.getFlavors(); //给集合中的每个菜品口味都附上菜品id值 flavors=flavors.stream().map((item)->{ item.setDishId(dishId); return item; }).collect(Collectors.toList()); //保存菜品口味数据到菜品口味表dish_flavor dishFlavorService.saveBatch(flavors); } }
DishController:
@RestController @RequestMapping("/dish") public class DishController { @Autowired private DishService dishService; @Autowired private DishFlavorService dishFlavorService; @PostMapping public R save(@RequestBody DishDto dishDto){ dishService.saveWithFlavor(dishDto); return new R(1,"新增菜品成功"); } }
菜品分页查询
DishController:
@GetMapping("/page") public R page(int page,int pageSize,String name){ //构造分页构造器对象 Page<Dish> pageInfo =new Page<>(page,pageSize); Page<DishDto> dishDtoPage =new Page<>(); //条件构造前 LambdaQueryWrapper<Dish> queryWrapper =new LambdaQueryWrapper<>(); //添加过滤条件 queryWrapper.like(name!=null,Dish::getName,name); //排序条件 queryWrapper.orderByDesc(Dish::getUpdateTime); //执行分页查询 dishService.page(pageInfo,queryWrapper); //因为我们查询dish的集合中,只查到category的id,不包含category的categoryName //所以要用新建并装进DishDto集合中 //对象拷贝 ,排除records数据,pageInfo的records也就是 dish集合 BeanUtils.copyProperties(pageInfo,dishDtoPage,"records"); List<Dish> records =pageInfo.getRecords(); //把Dish集合的每一个dish都拷贝到新建的dishDto 然后封装数据返回dishDto的集合 List<DishDto> list = records.stream().map((item) -> { DishDto dishDto = new DishDto(); BeanUtils.copyProperties(item, dishDto); Long categoryId = item.getCategoryId(); //根据Id查询category对象 然后获得name Category category = categoryService.getById(categoryId); //获取到name并封装进dishDto String categoryName = category.getName(); dishDto.setCategoryName(categoryName); return dishDto; }).toList(); //设置封装的Page类的Records dishDtoPage.setRecords(list); return new R(1,"查询成功",dishDtoPage); }
修改菜品
DishServiceImpl:
@Override public void updateWithFlavor(DishDto dishDto) { log.info("dishDto:{}",dishDto); //更新dish表基本信息 this.updateById(dishDto); //清理当前菜品对应口味数据---dish_flavor标的delete操作 LambdaQueryWrapper<DishFlavor> queryWrapper= new LambdaQueryWrapper<>(); queryWrapper.eq(DishFlavor::getDishId,dishDto.getId()); dishFlavorService.remove(queryWrapper); //添加当前菜品对应口味数据--- dish_flavor 的insert操作 List<DishFlavor> flavors = dishDto.getFlavors(); //给集合中的每个菜品口味都附上菜品id值 flavors=flavors.stream().map((item)->{ item.setDishId(dishDto.getId()); return item; }).collect(Collectors.toList()); dishFlavorService.saveBatch(flavors); }
DishController:
@PutMapping() public R update(@RequestBody DishDto dishDto){ dishService.updateWithFlavor(dishDto); return new R(1,"新增菜品成功"); }
菜品状态修改以及菜品删除
菜品状态修改
菜品状态,在前端post
传了一个status,跟菜品的当前状态相反
还有一个ids,也就是菜品的id
直接写DishController方法:
@PostMapping("/status/{status}") public R statusChange(@PathVariable int status, Long ids) { log.info("status:{},ids:{}",status,ids); Dish dish =new Dish(); dish.setStatus(status); UpdateWrapper<Dish> updateWrapper =new UpdateWrapper<>(); updateWrapper.eq("id",ids); dishService.update(dish,updateWrapper); log.info("dish:{}",dish); return new R(1,"状态修改成功"); }
菜品删除
菜品删除前,我们需要把菜品对应的口味删除掉
前端只传过来一个id 用的Delete请求
我们在DishService层写一个deleteWithFlavor方法
@Override public void deleteWithFlavor(Long id) { //清除当前菜品对应口味数据 LambdaQueryWrapper<DishFlavor> queryWrapper =new LambdaQueryWrapper<>(); queryWrapper.eq(DishFlavor::getDishId,id); dishFlavorService.remove(queryWrapper); //删除当前菜品数据 this.removeById(id); }
然后再在controller调用就可以了
@DeleteMapping public R deleteById(Long ids){ dishService.deleteWithFlavor(ids); return new R(1,"删除成功"); }