瑞吉外卖Day5
新增套餐
将新增页面录入的套餐信息插入到setmeal表,同时向setmeal_dish表插入套餐和菜品关联数据。
新增套餐涉及到两个表:
- setmeal 套餐表
- setmeal_dish 套餐菜品关系表
一、类和接口的基本结构
- 实体类SetmealDish
- DTO SetmealDto
- Mapper接口 SetmealDishMapper
- 业务层接口 SetmealDishService
- 业务层实现类SetmealDishServicelmpl
- 控制层 SetmealController
二、前端页面交互过程
- 页面(backend/page/combo/add.html)发送ajax请求,请求服务端获取套餐分类数据并展示到下拉框中
- 页面发送ajax请求,请求服务端获取菜品分类数据并展示到添加菜品窗口中
- 页面发送ajax请求,请求服务端,根据菜品分类查询对应的菜品数据并展示到添加菜品窗口中
- 页面发送请求进行图片上传,请求服务端将图片保存到服务器
- 页面发送请求进行图片下载,将上传的图片进行回显
- 点击保存按钮,发送ajax请求,将套餐相关数据以json形式提交到服务端
三、代码实现
1.实体类SetmealDish
代码语言:javascript复制package com.study.pojo;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 套餐菜品关系
*/
@Data
public class SetmealDish implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//套餐id
private Long setmealId;
//菜品id
private Long dishId;
//菜品名称 (冗余字段)
private String name;
//菜品原价
private BigDecimal price;
//份数
private Integer copies;
//排序
private Integer sort;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(fill = FieldFill.INSERT)
private Long createUser;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Long updateUser;
//是否删除
private Integer isDeleted;
}
2.Mapper接口 SetmealDishMapper
代码语言:javascript复制package com.study.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.study.pojo.SetmealDish;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SetmealDishMapper extends BaseMapper<SetmealDish> {
3.业务层接口 SetmealDishService
代码语言:javascript复制package com.study.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.study.pojo.SetmealDish;
public interface SetmealDishService extends IService<SetmealDish> {
}
4.业务层实现类SetmealDishServicelmpl
代码语言:javascript复制package com.study.Service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.Service.SetmealDishService;
import com.study.mapper.SetmealDishMapper;
import com.study.pojo.SetmealDish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class SetmealDishServiceimpl extends ServiceImpl<SetmealDishMapper, SetmealDish> implements SetmealDishService {
}
5.控制层 SetmealController
代码语言:javascript复制package com.study.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.Dto.SetmealDto;
import com.study.Service.CategoryService;
import com.study.Service.SetmealDishService;
import com.study.Service.SetmealService;
import com.study.common.R;
import com.study.pojo.Category;
import com.study.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
@Autowired
private SetmealService setmealService;
/**
* 新增套餐
* @param setmealDto
* @return
*/
@PostMapping
public R<String> save(@RequestBody SetmealDto setmealDto) {
log.info("套餐信息:{}",setmealDto);
setmealService.saveWithDish(setmealDto);
return R.success("新增套餐成功");
}
}
分页查询
一、前端和服务端交互过程
- 页面(backend/page/combo/list.html)发送ajax请求,将分页查询参数(page、pageSize、name)提交到服务端,获取分页数据页面发送请求
- 请求服务端进行图片下载,用于页面图片展示开发套餐信息分页查询功能
二、代码实现
SetmealController类中增添以下代码
代码语言:javascript复制 /**
* 套餐分页查询
* @param page
* @param pageSize
* @param name
* @return
*/
@GetMapping("/page")
public R<Page> page(int page,int pageSize,String name){
Page<Setmeal> pageInfo=new Page<>();
Page<SetmealDto> dtoPage=new Page<>();
LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.like(name!=null,Setmeal::getName,name);
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(pageInfo,queryWrapper);
BeanUtils.copyProperties(pageInfo,dtoPage,"records");
List<Setmeal> records=pageInfo.getRecords();
List<SetmealDto> list=records.stream().map((item)->{
SetmealDto setmealDto=new SetmealDto();
BeanUtils.copyProperties(item,setmealDto);
Long categoryId = item.getCategoryId();
Category category=categoryService.getById(categoryId);
if(category!=null){
String categoryName=category.getName();
setmealDto.setCategoryName(categoryName);
}
return setmealDto;
}).collect(Collectors.toList());
dtoPage.setRecords(list);
return R.success(dtoPage);
}
删除功能
一、前后端交互过程
1、删除单个套餐时,页面发送ajax请求,根据套餐id删除对应套餐 Request URL: http://localhost:8080/setmeal?ids=1414118011303899137
Request Method: DELETE
2、删除多个套餐时,页面发送ajax请求,根据提交的多个套餐id删除对应套餐 Request URL: http://localhost:8080/setmeal?ids=1414131634248101890,1414118011303899137 Request Method: DELETE
二、代码开发
SetmealController类中增添以下代码
代码语言:javascript复制/**
* 套餐删除
* @param ids
* @return
*/
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
log.info("ids:{}",ids);
setmealService.removeWithDish(ids);
return R.success("套餐删除成功");
}
完善其他功能
一、查询功能
代码语言:javascript复制 @GetMapping("list")
public R<List<Setmeal>> list(Setmeal setmeal){
log.info("setmeal:{}",setmeal);
LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.like(!StringUtils.isEmpty(setmeal.getName()),Setmeal::getName,setmeal.getName());
queryWrapper.eq(null!=setmeal.getCategoryId(),Setmeal::getCategoryId,setmeal.getCategoryId());
queryWrapper.eq(null!=setmeal.getStatus(),Setmeal::getStatus,setmeal.getStatus());
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
return R.success(setmealService.list(queryWrapper));
}
二、修改套餐状态功能
代码语言:javascript复制@PostMapping("/status/{st}")
public R<String> setStatus(@PathVariable int st, String ids){
//处理string 转成Long
String[] split = ids.split(",");
List<Long> idList = Arrays.stream(split).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
//将每个id new出来一个Dish对象,并设置状态
List<Setmeal> setmeals = idList.stream().map((item) -> {
Setmeal dish = new Setmeal();
dish.setId(item);
dish.setStatus(st);
return dish;
}).collect(Collectors.toList()); //Dish集合
log.info("status ids : {}",ids);
setmealService.updateBatchById(setmeals);//批量操作
return R.success("操作成功");
}
阿**平台
一、平台步骤
登录阿**–>短信服务–>免费开通–>Accesskey管理设置key–>申请签名和模板
二、JavaAPI
代码语言:javascript复制package com.study.utils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
/**
* 短信发送工具类
*/
public class SMSUtils {
/**
* 发送短信
* @param signName 签名
* @param templateCode 模板
* @param phoneNumbers 手机号
* @param param 参数
*/
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tQVLLFZqKHYFZ9qxpvk",
"bpZLPBlvZ4ZOpgoC5Ru6pRVBTZgd7S");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
request.setPhoneNumbers(phoneNumbers);
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setVersion("2017-05-25");
request.setTemplateParam("{"code":"" param ""}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
}catch (ClientException e) {
e.printStackTrace();
}
}
}
代码语言:javascript复制package com.study.utils;
import java.util.Random;
/**
* 随机生成验证码工具类
*/
public class ValidateCodeUtils {
/**
* 随机生成验证码
* @param length 长度为4位或者6位
* @return
*/
public static Integer generateValidateCode(int length){
Integer code =null;
if(length == 4){
code = new Random().nextInt(9999);//生成随机数,最大为9999
if(code < 1000){
code = code 1000;//保证随机数为4位数字
}
}else if(length == 6){
code = new Random().nextInt(999999);//生成随机数,最大为999999
if(code < 100000){
code = code 100000;//保证随机数为6位数字
}
}else{
throw new RuntimeException("只能生成4位或6位数字验证码");
}
return code;
}
/**
* 随机生成指定长度字符串验证码
* @param length 长度
* @return
*/
public static String generateValidateCode4String(int length){
Random rdm = new Random();
String hash1 = Integer.toHexString(rdm.nextInt());
String capstr = hash1.substring(0, length);
return capstr;
}
}