外卖项目day03-修改菜品-代码开发--2
This commit is contained in:
@@ -58,7 +58,17 @@ public class DishController {
|
|||||||
@ApiOperation("根据id查询菜品")
|
@ApiOperation("根据id查询菜品")
|
||||||
public Result<DishVO> getById(@PathVariable Long id){
|
public Result<DishVO> getById(@PathVariable Long id){
|
||||||
log.info("根据id查询菜品:{}",id);
|
log.info("根据id查询菜品:{}",id);
|
||||||
//todo
|
DishVO dishVO = dishService.getByIdWithFlavor(id);
|
||||||
|
return Result.success(dishVO);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
@ApiOperation("根据id修改菜品")
|
||||||
|
public Result update(@RequestBody DishDTO dto){
|
||||||
|
log.info("根据id修改菜品:{}",dto);
|
||||||
|
//调用业务层修改菜品
|
||||||
|
dishService.updateWithFlavors(dto);
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.mapper;
|
|||||||
import com.sky.entity.DishFlavor;
|
import com.sky.entity.DishFlavor;
|
||||||
import org.apache.ibatis.annotations.Delete;
|
import org.apache.ibatis.annotations.Delete;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -20,4 +21,12 @@ public interface DishFlavorMapper {
|
|||||||
*/
|
*/
|
||||||
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
||||||
void deleteByDishId(Long dishId);
|
void deleteByDishId(Long dishId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id查询口味数据列表
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Select("select * from dish_flavor where dish_id = #{id}")
|
||||||
|
List<DishFlavor> getByDishId(Long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,11 @@ public interface DishMapper {
|
|||||||
*/
|
*/
|
||||||
@Delete("delete from dish where id = #{dishId}")
|
@Delete("delete from dish where id = #{dishId}")
|
||||||
void deleteById(Long dishId);
|
void deleteById(Long dishId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id修改菜品信息
|
||||||
|
* @param dish
|
||||||
|
*/
|
||||||
|
@AutoFill(OperationType.UPDATE) //springAOP统一进行的自动填充功能
|
||||||
|
void update(Dish dish);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.sky.service;
|
|||||||
import com.sky.dto.DishDTO;
|
import com.sky.dto.DishDTO;
|
||||||
import com.sky.dto.DishPageQueryDTO;
|
import com.sky.dto.DishPageQueryDTO;
|
||||||
import com.sky.result.PageResult;
|
import com.sky.result.PageResult;
|
||||||
|
import com.sky.vo.DishVO;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -24,4 +25,17 @@ public interface DishService {
|
|||||||
* @param ids 要删除的菜品id
|
* @param ids 要删除的菜品id
|
||||||
*/
|
*/
|
||||||
void deleteBatch(List<Long> ids);
|
void deleteBatch(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id查询菜品信息并携带口味数据!!!
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
DishVO getByIdWithFlavor(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品
|
||||||
|
* @param dto
|
||||||
|
*/
|
||||||
|
void updateWithFlavors(DishDTO dto);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,4 +107,51 @@ public class DishServiceImpl implements DishService {
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据菜品id查询菜品信息并携带口味数据!!!
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public DishVO getByIdWithFlavor(Long id) {
|
||||||
|
//查询2张表:菜品表和口味表
|
||||||
|
//1.根据id查询菜品
|
||||||
|
Dish dish = dishMapper.getDishById(id);
|
||||||
|
//2.根据菜品id查询关联口味
|
||||||
|
List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
|
||||||
|
//3.将2表数据整合为一个DishVO对象
|
||||||
|
DishVO dishVO = new DishVO();
|
||||||
|
//3.1 将菜品表属性拷贝给vo对象
|
||||||
|
BeanUtils.copyProperties(dish,dishVO);
|
||||||
|
//3.2 将菜品口味表数据设置给vo对象
|
||||||
|
dishVO.setFlavors(dishFlavors);
|
||||||
|
return dishVO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改菜品
|
||||||
|
* @param dto
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional
|
||||||
|
public void updateWithFlavors(DishDTO dto) {
|
||||||
|
Long dishId = dto.getId();
|
||||||
|
//影响几张表??菜品表和口味表
|
||||||
|
Dish dish = new Dish();
|
||||||
|
BeanUtils.copyProperties(dto,dish);
|
||||||
|
//1.修改菜品表
|
||||||
|
dishMapper.update(dish);
|
||||||
|
//2.修改口味表:直接将表中跟菜品有关的口味,直接全部删除,根据传入的口味重新新增即可
|
||||||
|
dishFlavorMapper.deleteByDishId(dishId);
|
||||||
|
//3.批量新增口味
|
||||||
|
List<DishFlavor> flavors = dto.getFlavors();
|
||||||
|
if (flavors != null && flavors.size() > 0) {
|
||||||
|
//遍历每个口味,将菜品id,主动设置一遍
|
||||||
|
for (DishFlavor flavor : flavors) {
|
||||||
|
flavor.setDishId(dishId);
|
||||||
|
}
|
||||||
|
dishFlavorMapper.insertBath(flavors);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,20 @@
|
|||||||
insert into dish (name, category_id, price, image, description, create_time, update_time, create_user, update_user,status)
|
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})
|
values (#{name},#{categoryId},#{price},#{image},#{description},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})
|
||||||
</insert>
|
</insert>
|
||||||
|
<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>
|
||||||
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
<select id="pageQuery" resultType="com.sky.vo.DishVO">
|
||||||
select d.*,c.name as categoryName from dish d left join category c on d.category_id = c.id
|
select d.*,c.name as categoryName from dish d left join category c on d.category_id = c.id
|
||||||
<where>
|
<where>
|
||||||
|
|||||||
Reference in New Issue
Block a user