这是第二章
第三章:文件上传 以及文件下载
文件上传我们知道是post请求 因为Get请求传输数据最大为2KB 而Post请求大得多
@PostMapping("upload") //参数名称一定要是file不然传输不过去
public R<String> upload(MultipartFile file) {
//想用原来的文件名 也可以 不建议 有重名的
String originalFilename = file.getOriginalFilename();
//使用uuid重新生成文件名 防止覆盖 获取最后的一个 . 的下标
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
//用uuid随机生成 把后缀给截取上
String fileName = UUID.randomUUID().toString()+suffix;
// 创建文件的路径
File dir = new File(basePath);
//判断文件夹是否存在 如果不存在就创建一个
if (!dir.exists()) {
dir.mkdirs();
}
try {
//因为是这个tomcat是springboot自带的 需要把临时的文件转移到自定义的地方
file.transferTo(new File(basePath + fileName));
} catch (IOException e) {
e.printStackTrace();
}
//返回
return R.success(fileName);
}
文件下载
@GetMapping("download")
//前端传递过来的参数 向浏览器写入
public void download(String name, HttpServletResponse response) {
FileInputStream fileInputStream = null;
ServletOutputStream outputStream = null;
try {
//输入流 读取文件内容
fileInputStream = new FileInputStream(basePath + name);
//输出流 通过输出流将文件写回浏览器 在浏览器展示图片 我们要向浏览器写数据
outputStream = response.getOutputStream();
//设置内容的类型
response.setContentType("image/jpeg");
//使用输入流输入
byte[] bytes = new byte[1024];
int len;
//循环 只要不等于-1就一直循环
while ((len = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
菜品管理的菜品分类 让他显示到下拉框中
@GetMapping("list")
//查询菜品
public R<List<Category>> list(Category category){
//分类 做一个条件分类
LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
// 条件 值
queryWrapper.eq(Category::getType,category.getType());
queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
List<Category> list = categoryService.list(queryWrapper);
return R.success(list);
}
菜品管理的分页查询 前端传输过来的数据可知需要三个参数 size pageSize name
需要两张表Dish和dishFlavor 像口味dish表里边没有 我们需要建立一个dto
@GetMapping("page")
public R<Page> page(int page, int pageSize, String name) {
//构造分页构造器对象
Page<Dish> pageInfo = new Page<>(page, pageSize);
//条件构造器
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(name != null, Dish::getName, name);
//执行分页查询
dishService.page(pageInfo, queryWrapper);
System.out.println("================================");
//如果只写上边可得 除了菜品分类其他都有 我们需要把菜品分类也赋值 我们就需要DishDto 实体类
Page<DishDto> pageDto = new Page();
//拷贝 用BeanUtils工具类 把pageInfo里边的东西都拷贝到pageDto里边 records除外 为什么 因为pageInfo里边就是records
BeanUtils.copyProperties(pageInfo,pageDto,"records");
//用pageinfo获取
List<Dish> records = pageInfo.getRecords();
//用lambda表达式遍历
List<DishDto> list=records.stream().map((e)->{
//new 出来的 其他的值全部都是空的
DishDto dishDto=new DishDto();
因为我们是new出来 把dish里边的东西全部拷贝到dishDto里边
BeanUtils.copyProperties(e,dishDto);
Long categoryId = e.getCategoryId();//分类id
//根据id查询分类对象 也就是name 湘菜或者豫菜之类的
Category category = categoryService.getById(categoryId);
//获取名称
//如果为空就不设置值
if (category!=null) {
String categoryName = category.getName();
dishDto.setCategoryName(categoryName);
}
return dishDto;
}).collect(Collectors.toList());
pageDto.setRecords(list);
return R.success(pageDto);
}
课后作业**删除该分类**
@DeleteMapping
public R<String> delete(Long ids) {
dishService.remove(ids);
return R.success("成功");
}
停售该分类
@PostMapping("status/{status}")
public R<String> stops(@RequestParam("ids") List<Long> ids, @PathVariable int status) {
Dish dish = new Dish();
for (Long id : ids) {
dish.setId(id);
dish.setStatus(status);
dishService.updateById(dish);
}
return R.success("成功");
}