家住在女贞路 4 号的德思礼夫妇…
一、上传、下载
1. 接收上传文件
新建 src/main/java/com/reggie/controller/CommonController
类:
package com.reggie.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
/**
* 文件的上传和下载
*/
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("/upload")
public R<String> upload(MultipartFile file ) {
System.out.println(file);
return null;
}
}
2. 文件上传服务端
编辑 src/main/java/com/reggie/controller/CommonController
类:
...
/**
* 文件的上传和下载
*/
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("/upload")
public R<String> upload(MultipartFile file ) {
log.info(file.toString());
// 获取原始文件文件名
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
// 使用 UUID重新生成文件名,防止文件名称重复造成we文件覆盖
String fileName = UUID.randomUUID().toString() + suffix;
// 创建一个目录对象
File dir = new File(basePath);
// 判断当前目录是否存在
if(!dir.exists()) {
// 目录不存在,需要创建
dir.mkdirs();
}
// 将临时文件转存到指定路径
try {
file.transferTo(new File(basePath + fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
return R.success(fileName);
}
}
3. 文件下载
编辑 src/main/java/com/reggie/controller/CommonController
类:
...
/**
* 文件下载
* @param name
* @param response
*/
@GetMapping("/download")
public void download(String name, HttpServletResponse response) {
try {
// 输入流,读取文件内容
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
// 输出流,通过输出流将文件写回浏览器
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
// 关闭资源
fileInputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
...
4. 测试文件(前端)
新建 src/main/resources/backend/page/demo/upload.html
测试页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="../../plugins/element-ui/index.css" />
<link rel="stylesheet" href="../../styles/common.css" />
<link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
<div class="addBrand-container" id="food-add-app">
<div class="container">
<el-upload class="avatar-uploader"
action="/common/upload"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeUpload"
ref="upload">
<img v-if="imageUrl" :src="imageUrl" class="avatar"></img>
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/index.js"></script>
<script>
new Vue({
el: '#food-add-app',
data() {
return {
imageUrl: ''
}
},
methods: {
handleAvatarSuccess (response, file, fileList) {
this.imageUrl = `/common/download?name=${response.data}`
},
beforeUpload (file) {
if(file){
const suffix = file.name.split('.')[1]
const size = file.size / 1024 / 1024 < 2
if(['png','jpeg','jpg'].indexOf(suffix) < 0){
this.$message.error('上传图片只支持 png、jpeg、jpg 格式!')
this.$refs.upload.clearFiles()
return false
}
if(!size){
this.$message.error('上传文件大小不能超过 2MB!')
return false
}
return file
}
}
}
})
</script>
</body>
</html>
5. 相应配置
编辑 src/main/resources/application.yml
文件:
...
reggie:
path: D\img\
新建 src/test/java/UploadFileTest.java
文件:
import org.junit.jupiter.api.Test;
public class UploadFileTest {
@Test
public void text1(){
String fileName = "hello.jpg";
String suffix = fileName.substring(fileName.lastIndexOf("."));
}
}
6. 控制器方法
(结)编辑 src/main/java/com/reggie/controller/CommonController.java
类:
package com.reggie.controller;
import com.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
/**
* 文件的上传和下载
*/
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
@Value("${reggie.path}")
private String basePath;
/**
* 文件上传
* @param file
* @return
*/
@PostMapping("/upload")
public R<String> upload(MultipartFile file ) {
log.info(file.toString());
// 获取原始文件文件名
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
// 使用 UUID重新生成文件名,防止文件名称重复造成we文件覆盖
String fileName = UUID.randomUUID().toString() + suffix;
// 创建一个目录对象
File dir = new File(basePath);
// 判断当前目录是否存在
if(!dir.exists()) {
// 目录不存在,需要创建
dir.mkdirs();
}
// 将临时文件转存到指定路径
try {
file.transferTo(new File(basePath + fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
return R.success(fileName);
}
/**
* 文件下载
* @param name
* @param response
*/
@GetMapping("/download")
public void download(String name, HttpServletResponse response) {
try {
// 输入流,读取文件内容
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
// 输出流,通过输出流将文件写回浏览器
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
outputStream.flush();
}
// 关闭资源
fileInputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
二、新增菜品
1. 界面预览
2. 基础搭建
⑴. 实体类
新建 src/main/java/com/reggie/entity/DishFlavor
类:
package com.reggie.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
菜品口味
*/
@Data
public class DishFlavor implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//菜品id
private Long dishId;
//口味名称
private String name;
//口味数据list
private String value;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否删除
private Integer isDeleted;
}
⑵. Mapper 接口
新建 src/main/java/com/reggie/mapper/DishFlavorMapper
接口:
package com.reggie.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DishFlavorMapper extends BaseMapper<DishFlavor> {
}
⑶. Service 接口
新建 src/main/java/com/reggie/service/DishFlavorService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.entity.DishFlavor;
public interface DishFlavorService extends IService<DishFlavor> {
}
⑷. Service 实现类
新建 src/main/java/com/reggie/service/impl/DishFlavorServiceImpl
类:
package com.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.entity.DishFlavor;
import com.reggie.mapper.DishFlavorMapper;
import com.reggie.service.DishFlavorService;
import org.springframework.stereotype.Service;
@Service
public class DishFlavorServiceImpl extends ServiceImpl<DishFlavorMapper, DishFlavor> implements DishFlavorService {
}
3. 控制器
⑴. 菜品分类下拉
编辑 src/main/java/com/reggie/controller/CategoryController
类:
...
/**
* 根据条件查询分类数据
* @param category
* @return
*/
@GetMapping("/list")
public R<List<Category>> 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 R.success(list);
}
⑵. 导入 DTO
新建 src/main/java/com/reggie/dto/DishDto
类,用于封装页面提交的数据:
package com.reggie.dto;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class DishDto extends Dish {
private List<DishFlavor> flavors = new ArrayList<>();
private String categoryName;
private Integer copies;
}
⑶. 项目启动类
新增菜品时,需要向 dish 、 dish_flavor 两个表插入数据,为了保证数据一致性,需要启动事物管理
编辑 src/main/java/com/reggie/ReggieApplication
类:
//启动类
package com.reggie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Slf4j
@SpringBootApplication
@ServletComponentScan
@EnableTransactionManagement
public class ReggieApplication {
public static void main(String[] args) {
SpringApplication.run(ReggieApplication.class, args);
log.info("项目启动成功...");
}
}
⑷. 新增菜品
新增 src/main/java/com/reggie/controller/DishController
类:
package com.reggie.controller;
import ...
/**
* 菜品管理
*/
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
/**
* 新增菜品
* @param dishDto
* @return
*/
@PostMapping
public R<String> save(@RequestBody DishDto dish) {
log.info("dish: {}", dish);
return R.success("新增菜品成功");
}
}
⑸. 拓展 saveWithFlavor 方法
新建 src/main/java/com/reggie/service/DishService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
public interface DishService extends IService<Dish> {
// 新增菜品,同时插入菜品的口味数据,需要操作两张表:dish, dish_flavor
public void saveWithFlavor(DishDto dishDto);
}
⑹. 实现 saveWithFlavor 方法
新建 src/main/java/com/reggie/service/impl/DishServiceImpl
类:
package com.reggie.service.impl;
import ...
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
@Autowired
private DishFlavorService dishFlavorService;
@Autowired
private DishService dishService;
/**
* 新增菜品,同时保存对应的口味数据
* @param dishDto
*/
@Override
@Transactional
public void saveWithFlavor(DishDto dishDto) {
// 保存菜品的基本信息到菜品表 dish
this.save(dishDto);
Long dishId = dishDto.getId(); // 菜品
List<DishFlavor> flavors = dishDto.getFlavors(); // 菜品口味
// 菜品口味集合
flavors = flavors.stream().map((item) -> {
item.setDishId(dishId);
return item;
}).collect(Collectors.toList());
// 保存菜品口味数据到菜品口味数据表 dish_flavor
// dishFlavorService.saveBatch(dishDto.getFlavors());
dishFlavorService.saveBatch(flavors);
}
}
⑺. 完善控制层
编辑 src/main/java/com/reggie/controller/DishController
类:
package com.reggie.controller;
import ...
/**
* 菜品管理
*/
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
@Autowired
private DishService dishService;
@Autowired
private DishFlavorService dishFlavorService;
@Autowired
private CategoryService categoryService;
/**
* 新增菜品
* @param dishDto
* @return
*/
@PostMapping
public R<String> save(@RequestBody DishDto dishDto) {
log.info(dishDto.toString());
dishService.saveWithFlavor(dishDto);
return R.success("新增菜品成功");
}
}
三、分页查询
1. 界面预览
2. 编码实现
编辑 src/main/java/com/reggie/controller/DishController
类:
...
/**
* 菜品信息分页查询
* @param page
* @param pageSize
* @param name
* @return
*/
@GetMapping("/page")
public R<Page> 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);
// 对象拷贝
BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");
List<Dish> records = pageInfo.getRecords();
List<DishDto> list = records.stream().map((item) -> {
DishDto dishDto = new DishDto();
// 拷贝普通属性到 dishDto 对象中
BeanUtils.copyProperties(item, dishDto);
Long categoryId = item.getCategoryId(); // 分类 ID
// 根据 ID 查询分类对象
Category category = categoryService.getById(categoryId);
if(category != null) {
String categoryName = category.getName();
dishDto.setCategoryName(categoryName);
}
return dishDto;
}).collect(Collectors.toList());
dishDtoPage.setRecords(list);
return R.success(pageInfo);
}
}
四、修改菜品
1. 界面预览
2. 编码实现
⑴. 拓展 getByIdWithFlavor 方法
新建 src/main/java/com/reggie/service/DishService
类:
package com.reggie.service;
import ...
public interface DishService extends IService<Dish> {
// 新增菜品,同时插入菜品的口味数据,需要操作两张表:dish, dish_flavor
public void saveWithFlavor(DishDto dishDto);
// 根据 id 查询菜品信息和对应的口味信息
public DishDto getByIdWithFlavor(Long id);
}
⑵. 实现 getByIdWithFlavor 方法
新建 src/main/java/com/reggie/service/impl/DishServiceImpl
类:
...
/**
* 根据 id 查询菜品信息和口味信息
* @param id
* @return
*/
@Override
public DishDto getByIdWithFlavor(Long id) {
// 查询菜品的基本信息 dish
Dish dish = this.getById(id);
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(dish, dishDto);
// 查询当前菜品的口味信息 dish_flavor
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId, dish.getId());
List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
dishDto.setFlavors(flavors);
return dishDto;
}
}
⑶. 创建 get 方法
编辑 src/main/java/com/reggie/controller/DishController
类:
...
/**
* 根据 id 查询菜品信息和对应的口味信息
* @param id
* @return
*/
@GetMapping("/{id}")
public R<DishDto> get(@PathVariable Long id) {
DishDto dishDto = dishService.getByIdWithFlavor(id);
return R.success(dishDto);
}
}
⑷. 创建 updateWithFlavor 方法
新建 src/main/java/com/reggie/service/DishService
类:
package com.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
public interface DishService extends IService<Dish> {
// 新增菜品,同时插入菜品的口味数据,需要操作两张表:dish, dish_flavor
public void saveWithFlavor(DishDto dishDto);
// 根据 id 查询菜品信息和对应的口味信息
public DishDto getByIdWithFlavor(Long id);
// 更新菜品信息,同时更新对应的口味信息
public void updateWithFlavor(DishDto dishDto);
}
⑸. 实现 updateWithFlavor 方法
新建 src/main/java/com/reggie/service/impl/DishServiceImpl
类:
package com.reggie.service.impl;
import ...
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
@Autowired
private DishFlavorService dishFlavorService;
@Autowired
private DishService dishService;
/**
* 新增菜品,同时保存对应的口味数据
* @param dishDto
*/
@Override
@Transactional
public void saveWithFlavor(DishDto dishDto) {
// 保存菜品的基本信息到菜品表 dish
this.save(dishDto);
Long dishId = dishDto.getId(); // 菜品
List<DishFlavor> flavors = dishDto.getFlavors(); // 菜品口味
// 菜品口味集合
flavors = flavors.stream().map((item) -> {
item.setDishId(dishId);
return item;
}).collect(Collectors.toList());
// 保存菜品口味数据到菜品口味数据表 dish_flavor
// dishFlavorService.saveBatch(dishDto.getFlavors());
dishFlavorService.saveBatch(flavors);
}
/**
* 根据 id 查询菜品信息和口味信息
* @param id
* @return
*/
@Override
public DishDto getByIdWithFlavor(Long id) {
// 查询菜品的基本信息 dish
Dish dish = this.getById(id);
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(dish, dishDto);
// 查询当前菜品的口味信息 dish_flavor
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId, dish.getId());
List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
dishDto.setFlavors(flavors);
return dishDto;
}
/**
* 修改菜品信息
* @param dishDto
*/
@Override
@Transactional
public void updateWithFlavor(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();
flavors = flavors.stream().map((item) -> {
item.setDishId(dishDto.getId());
return item;
}).collect(Collectors.toList());
dishFlavorService.saveBatch(flavors);
}
}
⑹. 完善控制层
编辑 src/main/java/com/reggie/controller/DishController
类:
...
/**
* 修改菜品
* @param dishDto
* @return
*/
@PutMapping
public R<String> update(@RequestBody DishDto dishDto) {
log.info(dishDto.toString());
dishService.updateWithFlavor(dishDto);
return R.success("修改菜品成功");
}
}