一、创建用于显示到前端的厨艺秀DTO
名称:CookingSkillImagesDTO
/** * @Package: cn.liuliang.quickdinesysstore.entity.dto * @Author: liuliang * @CreateTime: 2020/10/28 - 13:56 * @Description: 厨艺秀展示dto */ @Data @ApiModel(value="厨艺秀展示dto", description="") public class CookingSkillImagesDTO implements Serializable { private static final long serialVersionUID=1L; @ApiModelProperty(value = "ID") private Long id; @ApiModelProperty(value = "照片url") private String imageUrl; }
二、编写Controller
名称:CookingSkillImagesController
/** * <p> * 前端控制器:图片秀管理控制器 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Api(value = "厨艺秀图片管理", tags = "厨艺秀图片管理") @CrossOrigin @RestController @RequestMapping("/cooking-skill-images") public class CookingSkillImagesController { @Autowired private CookingSkillImagesService cookingSkillImagesService; @ApiOperation(value = "分页查看所有厨艺秀图片", notes = "分页查看所有厨艺秀图片") @ApiResponses({@ApiResponse(code = 200, message = "请求成功!", response = CookingSkillImagesDTO.class)}) @GetMapping("/select-all") public ResultDTO selectAllCookingSkillImages( @ApiParam("当前页码") @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @ApiParam("每页显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) { return cookingSkillImagesService.selectPage(pageNum, pageSize); } @ApiOperation(value = "添加厨艺秀照片", notes = "添加厨艺秀照片") @PostMapping("/add") public ResultDTO addCookingSkillImages( @ApiParam("图片url数组") @RequestParam(value = "imageUrls", required = true) String[] imageUrls) { return cookingSkillImagesService.addCookingSkillImages(imageUrls); } @ApiOperation(value = "删除厨艺秀照片", notes = "删除厨艺秀照片") @PostMapping("/delete") public ResultDTO delete( @ApiParam("图片id") @RequestParam(value = "id", required = true) Long id, @ApiParam("图片url") @RequestParam(value = "url", required = true) String url) { return cookingSkillImagesService.delete(id, url); } }
三、编写Service
名称:CookingSkillImagesService
/** * <p> * 服务类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ public interface CookingSkillImagesService extends IService<CookingSkillImages> { /** * 分页查询厨艺秀图片列表 * @param pageNum 当前页 * @param pageSize 每页数量 * @return */ ResultDTO selectPage(Integer pageNum, Integer pageSize); /** * 添加图片url数组 * @param imageUrls 图片url数组 * @return */ ResultDTO addCookingSkillImages(String[] imageUrls); /** * 删除图片 * @param id 图片id * @param url 图片url * @return */ ResultDTO delete(Long id, String url); }
实现类:CookingSkillImagesServiceImpl
/** * <p> * 服务实现类 * </p> * * @author j3_liuliang * @since 2020-10-24 */ @Service public class CookingSkillImagesServiceImpl extends ServiceImpl<CookingSkillImagesMapper, CookingSkillImages> implements CookingSkillImagesService { @Autowired private CookingSkillImagesMapper cookingSkillImagesMapper; @Autowired private OSSFileService ossFileService; @Override public ResultDTO selectPage(Integer pageNum, Integer pageSize) { // 定义分页 Page<CookingSkillImages> cookingSkillImagesPage = new Page<>(pageNum, pageSize); // 开始分页查询 IPage<CookingSkillImages> cookingSkillImagesPageModel = cookingSkillImagesMapper.selectPage(cookingSkillImagesPage, null); // 获取分页数据和总条数 List<CookingSkillImages> records = cookingSkillImagesPageModel.getRecords(); long total = cookingSkillImagesPageModel.getTotal(); // 转化数据传输对象 List<CookingSkillImagesDTO> cookingSkillImagesDTOList = new ArrayList<>(records.size()); records.forEach(CookingSkillImages -> { CookingSkillImagesDTO cookingSkillImagesDTO = new CookingSkillImagesDTO(); BeanUtils.copyProperties(CookingSkillImages, cookingSkillImagesDTO); cookingSkillImagesDTOList.add(cookingSkillImagesDTO); }); // 返回结果 return ResultDTO.success().data("tital", total).data("rows", cookingSkillImagesDTOList); } @Override public ResultDTO addCookingSkillImages(String[] imageUrls) { // 遍历url数组,依次插入数据库中 Arrays.asList(imageUrls).forEach(imageUrl -> { CookingSkillImages cookingSkillImages = new CookingSkillImages(); cookingSkillImages.setImageUrl(imageUrl); cookingSkillImagesMapper.insert(cookingSkillImages); }); return ResultDTO.success(); } @Override public ResultDTO delete(Long id, String url) { // 先删除数据库中的图片 cookingSkillImagesMapper.deleteById(id); // 再删除OSS服务器上的图片 ossFileService.removeFile(url); return ResultDTO.success(); } }
四、编写mapper及相关mapper映射文件
名称:CookingSkillImagesMapper
/** * <p> * Mapper 接口 * </p> * * @author j3_liuliang * @since 2020-10-24 */ public interface CookingSkillImagesMapper extends BaseMapper<CookingSkillImages> { }
名称:CookingSkillImagesMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.liuliang.quickdinesysstore.mapper.CookingSkillImagesMapper"> </mapper>
这里面持久成都不用编写相关的代码,是因为用来MyBatisPlus可以简化我们很多的简单访问数据库操作,实我们可以更多的关注到业务逻辑上而不是持久化上
五、启动项目swagger测试
访问:http://localhost:9110/swagger-ui.html#!
厨艺秀的相关功能就写的差不多了,下次开始些菜品管理相关的内容
结束语
- 由于博主才疏学浅,难免会有纰漏,假如你发现了错误或偏见的地方,还望留言给我指出来,我会对其加以修正。
- 如果你觉得文章还不错,你的转发、分享、点赞、留言就是对我最大的鼓励。
- 感谢您的阅读,十分欢迎并感谢您的关注。