欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > 苍穹外卖 新增套餐接口

苍穹外卖 新增套餐接口

2024/10/27 7:33:50 来源:https://blog.csdn.net/Aishangyuwen/article/details/143261959  浏览:    关键词:苍穹外卖 新增套餐接口

        新增套餐主要的坑:新增时操作数据库,不能使用简单的@Insert注解,因为要使用到数据库自增的id值,所以说必须使用XML配置数据库;必须要注意建立好套餐和对应菜品之间的关联。

        SetmealController

package com.sky.controller.admin;import com.sky.dto.SetmealDTO;
import com.sky.result.Result;
import com.sky.service.SetMealService;
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.data.redis.core.RedisTemplate;
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/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetMealController {@Autowiredprivate SetMealService setMealService;@Autowiredprivate RedisTemplate redisTemplate;/*** 新增套餐接口** @param setmealDTO* @return*/@PostMapping@ApiOperation("新增套餐")public Result save(@RequestBody SetmealDTO setmealDTO) {log.info("新增套餐:{}", setmealDTO);setMealService.save(setmealDTO);return Result.success();}
}

        SetmealService

/*** 新增套餐** @param setmealDTO*/@Overridepublic void save(SetmealDTO setmealDTO) {// 将SetmealDTO对象封装为对应的setmealDTO对象Setmeal setmeal = new Setmeal();BeanUtils.copyProperties(setmealDTO, setmeal);// 因为有AOP编程,所以说不需要再为setmeal对象补充属性了// 直接调用Mapper中的方法添加新的套餐在数据库中setMealMapper.insert(setmeal);// 需要将套餐和对应的菜品建立联系,所以说需要将套餐的id和其对应的菜品id添加到setmeal_dish表中// 获取这次请求添加的套餐的idLong setmealId = setmeal.getId();log.info("setmealID:{}", setmealId);// setmealDTO中的setmealdishes是一个arraylist集合,其中存储的是SetmealDish对象,// 对象中有菜品的信息,需要为其补充菜品所属的套餐id// 获取当前套餐下存储的setmealDish的信息List<SetmealDish> setmealDishs = setmealDTO.getSetmealDishes();// 为当前套餐下存储的所有setmealDish关联当前套餐的idsetmealDishs.forEach(new Consumer<SetmealDish>() {@Overridepublic void accept(SetmealDish setmealDish) {setmealDish.setSetmealId(setmealId);}});// 批量保存套餐和菜品的关联关系setmealDishMapper.insertBatch(setmealDishs);}

        SetmealMapper

 /*** 插入一个新套餐** @param setmeal*/// TODO重大BUG,因为要使用自动自增的id给当前的setmeal对象,所以说不能直接使用@Insert注解,而必须使用xml配置SQL// useGeneratedKeys="true":表示在插入数据后,要使用数据库生成的键值(通常是自增主键)// keyProperty="id":指定将数据库生成的键值赋值给传入参数对象的id属性,这样才能将自增的id给setmeal对象@AutoFill(value = OperationType.INSERT)void insert(Setmeal setmeal);
<insert id="insert" parameterType="Setmeal" useGeneratedKeys="true" keyProperty="id">insert into setmeal(category_id, name, price, status, description, image, create_time, update_time, create_user, update_user)values (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime},#{createUser}, #{updateUser})</insert>

 

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com