新增菜品业务代码开发
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.sky;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sky.config;
|
||||
|
||||
|
||||
import com.sky.properties.AliOssProperties;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 配置类 用于创建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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.utils.AliOssUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 通用接口
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.DishService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* 菜品管理
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
|
||||
import com.sky.entity.DishFlavor;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface DishFlavorMapper {
|
||||
|
||||
/**
|
||||
* 批量插入口味数据
|
||||
* @param flavors
|
||||
*/
|
||||
void insertBatch(List<DishFlavor> flavors);
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.annotation.AutoFill;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@@ -14,4 +17,11 @@ public interface DishMapper {
|
||||
@Select("select count(id) from dish where category_id = #{categoryId}")
|
||||
Integer countByCategoryId(Long categoryId);
|
||||
|
||||
|
||||
/**
|
||||
* 插入菜品数据
|
||||
* @param dish
|
||||
*/
|
||||
@AutoFill(value = OperationType.INSERT) //公共字段自动填充 不需要手动赋值
|
||||
void insert(Dish dish);
|
||||
}
|
||||
|
||||
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
13
sky-server/src/main/java/com/sky/service/DishService.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
|
||||
public interface DishService {
|
||||
|
||||
|
||||
/**
|
||||
* 新增菜品和对应的口味数据
|
||||
* @param dishDTO
|
||||
*/
|
||||
public void saveWithFlavor(DishDTO dishDTO);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import com.sky.mapper.DishFlavorMapper;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.service.DishService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
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);
|
||||
|
||||
|
||||
//向菜品表插入一条数据
|
||||
dishMapper.insert(dish);
|
||||
//获取insert语句生成的主键值
|
||||
Long dishId=dish.getId();
|
||||
|
||||
//向口味表插入多条数据
|
||||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||
if (flavors != null && flavors.size() > 0) {
|
||||
|
||||
flavors.forEach(dishFlavor -> {
|
||||
dishFlavor.setDishId(dishId);
|
||||
});
|
||||
//批量插入 而不是遍历
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,3 +6,9 @@ sky:
|
||||
database: sky_take_out
|
||||
username: root
|
||||
password: 1234
|
||||
|
||||
alioss:
|
||||
endpoint: oss-cn-beijing.aliyuncs.com
|
||||
access-key-id: LTAI5tPB4TemZaPXduvaV89G
|
||||
access-key-secret: 0oqBHnXTw4C2bIeuVdEJCpMNvXtaB5
|
||||
bucket-name: web-tlias-901
|
||||
|
||||
@@ -37,3 +37,12 @@ sky:
|
||||
admin-ttl: 7200000
|
||||
# 设置前端传递过来的令牌名称
|
||||
admin-token-name: token
|
||||
|
||||
|
||||
|
||||
#阿里云配置
|
||||
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}
|
||||
|
||||
12
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
12
sky-server/src/main/resources/mapper/DishFlavorMapper.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?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>
|
||||
12
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
12
sky-server/src/main/resources/mapper/DishMapper.xml
Normal file
@@ -0,0 +1,12 @@
|
||||
<?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>
|
||||
Reference in New Issue
Block a user