苍穹外卖

新增菜品:

需求分析与设计:

产品原型:

接口设计:

表设计:

通过原型图进行分析:

菜品表:dish

字段名 数据类型 说明 备注
id bigint 主键 自增
name varchar(32) 菜品名称 唯一
category_id bigint 分类 id 逻辑外键
price decimal(10,2) 菜品价格
image varchar(255) 图片路径
description varchar(255) 菜品描述
status int 售卖状态 1 起售 0 停售
create_time datetime 创建时间
update_time datetime 最后修改时间
create_user bigint 创建人id
update_user bigint 最后修改人id

菜品口味表:dish_flavor

字段名 数据类型 说明 备注
id bigint 主键 自增
dish_id bigint 菜品id 逻辑外键
name varchar(32) 口味名称
value varchar(255) 口味值

代码开发:

文件上传:

文件上传,是指将本地图片、视频、音频等文件上传到服务器上,可以供其他用户浏览或下载的过程

实现文件上传服务的核心前提是具备可靠的存储支撑,目前主要有三种解决方案,各有优劣:

服务硬盘直接存储(基于 SpringMVC 文件上传):优势是开发便捷、零额外成本,劣势是后续系统扩容难度大,无法适配大规模部署场景

分布式文件系统存储(如 FastDFS、MinIO):优势是易于实现横向扩容,支撑海量文件存储,劣势是开发复杂度略高于本地存储,需搭建和维护分布式集群

第三方存储服务(如阿里云 OSS):优势是开发简单、功能强大且无需自行维护存储集群,劣势是需要按需付费

定义 OSS 相关配置:

application-dev.yml:

复制代码
sky:
  alioss:
    endpoint: 
    access-key-id: 
    access-key-secret: 
    bucket-name: 

application.yml:

复制代码
spring:
  profiles:
    active: dev    #设置环境
sky:
  alioss:
    endpoint: ${sky.alioss.endpoint}
    access-key-id: ${sky.alioss.access-key-id}
    access-key-secret: ${sky.alioss.access-key-secret}
    bucket-name: ${sky.alioss.bucket-name}

读取 OSS 配置:

在 sky-common 模块中,已定义 AliOssProperties:

java 复制代码
@Component
@ConfigurationProperties(prefix = "sky.alioss")
@Data
public class AliOssProperties{

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

}

生成 OSS 工具类对象:

在 sky-server 模块下 config 包创建:

java 复制代码
/**
 * 配置类,用于创建 AliOssUtil 对象
 */
@Configuration
@Slf4j
public class OssConfiguration{
    @Bean
    @ConditionalOnMissingBean
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
        log.info("开始创建阿里云文件上传工具类对象:{}",aliOssProperties);
        return new AliOssUtil(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret(), aliOssProperties.getBucketName());
    }
}

在 sky-common 模块中,已定义 AliOssUtil:

java 复制代码
@Data
@AllArgsConstructor
@Slf4j
public class AliOssUtil{

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes, String objectName){

        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try{
            //创建PutObject请求
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
        }catch(OSSException oe){
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        }catch(ClientException ce){
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        }finally{
            if (ossClient != null){
                ossClient.shutdown();
            }
        }

        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder
                .append(bucketName)
                .append(".")
                .append(endpoint)
                .append("/")
                .append(objectName);

        log.info("文件上传到:{}", stringBuilder.toString());

        return stringBuilder.toString();
    }
}

定义文件上传接口:

CommonController:

java 复制代码
/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController{
    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传:{}",file);
        try{
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀名
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(),objectName);
            return Result.success(filePath);
        }catch (IOException e){
            log.error("文件上传失败:{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}

增添菜品:

设计 DTO 类:

在 sky-pojo 模块中,已定义 DishDTO:

java 复制代码
@Data
public class DishDTO implements Serializable{

    private Long id;
    //菜品名称
    private String name;
    //菜品分类id
    private Long categoryId;
    //菜品价格
    private BigDecimal price;
    //图片
    private String image;
    //描述信息
    private String description;
    //0 停售 1 起售
    private Integer status;
    //口味
    private List<DishFlavor> flavors = new ArrayList<>();

}

Controller 层:

DishController:

java 复制代码
/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController{
    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }
}

Service 层:

DishService:

java 复制代码
public interface DishService{
    /**
     * 新增菜品和对应的口味
     * @param dishDTO
     */
    void saveWithFlavor(DishDTO dishDTO);
}

DishServiceImpl:

java 复制代码
@Service
@Slf4j
public class DishServiceImpl implements DishService{
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入1条数据
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(dishflavor->{
                dishflavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}

Mapper 层:

DishMapper:

java 复制代码
@Mapper
public interface DishMapper{
    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{category_id}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 插入菜品数据
     *
     * @param dish
     */
    @AutoFill(value = OperationType.INSERT)
    void insert(Dish dish);
}

DishFlavorMapper:

java 复制代码
@Mapper
public interface DishFlavorMapper{
    /**
     * 批量插入口味数据
     * @param flavors
     */
    void insertBatch(List<DishFlavor> flavors);
}

DishMapper.xml:

java 复制代码
<?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="com.sky.mapper.DishMapper">
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into dish(name,category_id,price,image,description,create_time,update_time,create_user,update_user,status)
        values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
    </insert>
</mapper>

DishFlavorMapper.xml

java 复制代码
<?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="com.sky.mapper.DishFlavorMapper">
    <insert id="insertBatch">
        insert into dish_flavor (dish_id,name,value) VALUES
            <foreach collection="flavors" item="df" separator=",">
                (#{df.dishId},#{df.name},#{df.value})
            </foreach>
    </insert>
</mapper>

功能测试:

代码提交:

菜品分页查询:

需求分析和设计:

产品原型:

接口设计:

代码开发:

设计 DTO 类:

在 sky-pojo 模块中,已定义 DishPageQueryDTO:

java 复制代码
@Data
public class DishPageQueryDTO implements Serializable{

    private int page;

    private int pageSize;

    private String name;

    //分类id
    private Integer categoryId;

    //状态 0表示禁用 1表示启用
    private Integer status;

}

设计 VO 类:

在 sky-pojo 模块中,已定义 DishVO:

java 复制代码
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DishVO implements Serializable{

    private Long id;
    //菜品名称
    private String name;
    //菜品分类id
    private Long categoryId;
    //菜品价格
    private BigDecimal price;
    //图片
    private String image;
    //描述信息
    private String description;
    //0 停售 1 起售
    private Integer status;
    //更新时间
    private LocalDateTime updateTime;
    //分类名称
    private String categoryName;
    //菜品关联的口味
    private List<DishFlavor> flavors = new ArrayList<>();

    //private Integer copies;
}

Controller 层:

在 DishController 中添加分页查询方法

java 复制代码
/**
 * 菜品分页查询
 *
 * @param dishPageQueryDTO
 * @return
 */
@GetMapping("/page")
@ApiOperation("菜品分页查询")
public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
log.info("菜品分页查询:{}",dishPageQueryDTO);
PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
return Result.success(pageResult);
}

Service 层:

在 DishService 中声明分页查询方法

java 复制代码
/**
 * 菜品分页查询
 *
 * @param dishPageQueryDTO
 * @return
 */
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);

在 DishServiceImpl 中实现分页查询方法

java 复制代码
/**
 * 菜品分页查询
 *
 * @param dishPageQueryDTO
 * @return
 */
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO){
    PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
    Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
    return new  PageResult(page.getTotal(),page.getResult());
}

Mapper 层:

在 DishMapper 接口中声明 pageQuery 方法

java 复制代码
/**
 * 菜品分页查询
 *
 * @param dishPageQueryDTO
 * @return
 */
Page<DishVO> pageQuery(DishPageQueryDTO dishPageQueryDTO);

在 DishMapper.xml 中编写 SQL

java 复制代码
<select id="pageQuery" resultType="com.sky.vo.DishVO">
select d.*,c.name as categoryName from dish d left outer join category c on d.category_id = c.id
<where>
    <if test="name != null">
        and d.name like concat('%',#{name},'%')
    </if>
    <if test="categoryId != null">
        and d.category_id = #{categoryId}
    </if>
    <if test="status != null">
        and d.status = #{status}
    </if>
</where>
order by d.create_time desc
</select>

功能测试:

接口文档测试:

前后端联调测试:

删除菜品:

需求分析和设计:

产品原型:

接口设计:

表设计:

代码开发:

Controller 层:

在 DishController 中添加批量删除方法

java 复制代码
/**
 * 菜品批量删除
 *
 * @param ids
 * @return
 */
@DeleteMapping
@ApiOperation("菜品批量删除")
public Result delete(@RequestParam List<Long> ids){
    log.info("菜品批量删除:{}",ids);
    dishService.deleteBatch(ids);
    return Result.success();
}

Service 层:

在 DishService 中声明批量删除方法

java 复制代码
/**
 * 菜品批量删除
 *
 * @param ids
 */
void deleteBatch(List<Long> ids);

在 DishServiceImpl 中实现批量删除方法

java 复制代码
/**
 * 菜品批量删除
 *
 * @param ids 待删除的菜品ID列表
 */
@Transactional
public void deleteBatch(List<Long> ids){
    //判断当前菜品是否能删除---是否存在起售中的菜品
    for(Long id : ids){
        Dish dish = dishMapper.getById(id);
        if (dish.getStatus() == StatusConstant.ENABLE){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
    }

    //判断当前菜品是否能删除---是否被套餐关联
    List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
    if (setmealIds != null && setmealIds.size() > 0){
        throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
    }

    //删除菜品表中的菜品及关联的口味数据
    for(Long id : ids){
        dishMapper.deleteById(id);
        dishFlavorMapper.deleteByDishId(id);
    }
}

Mapper 层:

在 DishMapper 中声明 getById 方法

java 复制代码
/**
 * 根据主键查询菜品
 *
 * @param id
 * @return
 */
@Select("select * from dish where id = #{id}")
Dish getById(Long id);

在 SetmealDishMapper 中声明 getSetmealIdsByDishIds 方法:

java 复制代码
@Mapper
public interface SetmealDishMapper{
    /**
     * 根据菜品id查询对应的套餐id
     *
     * @param dishIds
     * @return
     */
    List<Long> getSetmealIdsByDishIds(List<Long> dishIds);
}

在 SetmealDishMapper.xml 中编写 SQL

java 复制代码
<?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="com.sky.mapper.SetmealDishMapper">
    <select id="getSetmealIdsByDishIds" resultType="java.lang.Long">
        select setmeal_id from setmeal_dish where dish_id in 
            <foreach collection="dishIds" item="dishId" separator="," open="(" close=")">
                #{dishId}
            </foreach>
    </select>
</mapper>

在 DishMapper 中声明 deleteById 方法

java 复制代码
/**
 * 根据主键删除菜品数据
 * 
 * @param id
 */
@Delete("delete from dish where id = #{id}")
void deleteById(Long id);

在 DishFlavorMapper 中声明 deleteByDishId 方法

java 复制代码
/**
 * 根据菜品id删除对应的口味数据
 * @param dishId
 */
@Delete("delete from dish_flavor where dish_id = #{dishId}")
void deleteByDishId(Long dishId);

功能测试:

代码提交:

修改代码:

需求分析和设计:

产品原型:

接口设计:

代码开发:

Controller 层:

在 DishController 中添加方法

java 复制代码
/**
 * 根据id查询菜品
 *
 * @param id
 * @return
 */
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public Result<DishVO> getById(@PathVariable Long id){
    log.info("根据id查询菜品:{}",id);
    DishVO dishVO = dishService.getByIdWithFlavor(id);
    return Result.success(dishVO);
}

/**
 * 修改菜品
 *
 * @param dishDTO
 * @return
 */
@PutMapping
public Result update(@RequestBody DishDTO dishDTO){
    log.info("修改菜品:{}",dishDTO);
    dishService.updateWithFlavor(dishDTO);
    return Result.success();
}

Service 层:

在 DishService 接口中声明方法

java 复制代码
/**
 * 根据id查询菜品和对应的口味数据
 *
 * @param id
 * @return
 */
DishVO getByIdWithFlavor(Long id);

/**
 * 根据id修改菜品基本信息和对应的口味信息
 *
 * @param dishDTO
 */
void updateWithFlavor(DishDTO dishDTO);

在 DishServiceImpl 中实现 方法

java 复制代码
/**
 * 根据id查询菜品和对应的口味数据
 *
 * @param id
 * @return
 */
public DishVO getByIdWithFlavor(Long id){
    //根据id查询菜品数据
    Dish dish = dishMapper.getById(id);

    //根据菜品查询口味数据
    List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);

    //将查询到的数据封装到VO
    DishVO dishVO = new DishVO();
    BeanUtils.copyProperties(dish,dishVO);
    dishVO.setFlavors(dishFlavors);

    return dishVO;
}

/**
 * 根据id修改菜品基本信息和对应的口味信息
 *
 * @param dishDTO
 */
public void updateWithFlavor(DishDTO dishDTO){
    Dish dish = new Dish();
    BeanUtils.copyProperties(dishDTO,dish);

    //修改菜品表基本信息
    dishMapper.update(dish);

    //删除原有的口味数据
    dishFlavorMapper.deleteByDishId(dish.getId());

    //重写插入口味数据
    List<DishFlavor> flavors = dishDTO.getFlavors();
    if(flavors!=null && flavors.size()>0){
        flavors.forEach(dishflavor->{
            dishflavor.setDishId(dish.getId());
        });
        //向口味表插入n条数据
        dishFlavorMapper.insertBatch(flavors);
    }
}

Mapper 层:

在 DishMapper 中声明方法

java 复制代码
/**
 * 根据主键删除菜品数据
 *
 * @param id
 */
@Delete("delete from dish where id = #{id}")
void deleteById(Long id);

/**
 * 根据id动态修改菜品数据
 *
 * @param dish
 */
// 错误:更新操作应使用 OperationType.UPDATE,而非 INSERT
@AutoFill(value = OperationType.UPDATE)
void update(Dish dish);

在 DishMapper.xml 中编写 SQL

java 复制代码
<update id="update">
    update dish
    <set>
        <if test="name != null">name = #{name},</if>
        <if test="categoryId != null">category_id = #{categoryId},</if>
        <if test="price != null">price = #{price},</if>
        <if test="image != null">image = #{image},</if>
        <if test="description != null">description = #{description},</if>
        <if test="status != null">status = #{status},</if>
        <if test="updateTime != null">update_time = #{updateTime},</if>
        <if test="updateUser != null">update_user = #{updateUser},</if>
    </set>
    where id = #{id}
</update>

功能测试:

代码提交:

相关推荐
短剑重铸之日2 小时前
《SpringCloud实用版》 Gateway 4.3.x 保姆级实战:路由 + 限流 + 鉴权 + 日志全覆盖
java·后端·spring cloud·架构·gateway
源代码•宸2 小时前
Golang原理剖析(彻底理解Go语言栈内存/堆内存、Go内存管理)
经验分享·后端·算法·面试·golang·span·mheap
高山上有一只小老虎2 小时前
mybatisplus实现简单的增删改查方法
java·spring boot·后端
计算机程序设计小李同学2 小时前
基于位置服务的二手图书回收平台
java·前端·vue.js·spring boot·后端
Java程序员威哥2 小时前
SpringBoot多环境配置实战:从基础用法到源码解析与生产避坑
java·开发语言·网络·spring boot·后端·python·spring
猿小羽3 小时前
[TEST] Spring Boot 快速入门指南 - 1769246843980
java·spring boot·后端
Anastasiozzzz3 小时前
常见限流算法--【令牌桶】【漏桶】【固定窗口】【滑动窗口】
java·redis·后端·算法·面试
Zfox_4 小时前
【Docker#2】容器化虚拟化
运维·后端·docker·容器
小北方城市网4 小时前
Spring Cloud Gateway实战:路由、限流、熔断与鉴权全解析
java·spring boot·后端·spring·mybatis