当前位置: 首页 > news >正文

springboot苍穹外卖实战:五、公共字段自动填充(aop切面实现)+新增菜品功能+oss

公共字段自动填充

不足

比起瑞吉外卖中的用自定义元数据类型+mybatisplus的实现,这里使用的是aop切面实现,会麻烦许多,建议升级为mp。

定义好数据库操作类型

sky-common中已经定义好,OperationType。

自定义注解 AutoFill

com.sky.annotation.AutoFill

/*** 自定义注解,用于标识某个方法需要进行功能字段自动填充处理*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {//数据库操作类型:UPDATE INSERTOperationType value();
}

自定义切面 AutoFillAspect

com.sky.aspect.AutoFillAspect


@Slf4j
public class AutoFillAspect {/*** 切入点*/@Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")public void autoFillPointCut(){}/*** 前置通知,在通知中进行公共字段的赋值*/@Before("autoFillPointCut()")public void autoFill(JoinPoint joinPoint){//可先进行调试,是否能进入该方法 提前在mapper方法添加AutoFill注解log.info("开始进行公共字段自动填充...");//步骤一:获取到当前被拦截的方法上的数据库操作类型MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获得方法签名对象AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象OperationType operationType = autoFill.value();//获得数据库操作类型//步骤二:获取到当前被拦截的方法的参数,即实体对象Object[] args = joinPoint.getArgs();if(args == null || args.length == 0){return;}Object entity = args[0];//步骤三:准备赋值的数据LocalDateTime now = LocalDateTime.now();Long currentid = BaseContext.getCurrentId();/*步骤四:根据当前不同的操作类型,为对应的属性通过反射来赋值。之所以不直接通过set等方法赋值,是因为获取到的实体类对象可能不一样。譬如这里该项目就可能为员工或者菜品使用set方法需要强转为某个实体类对象。*/if(operationType == OperationType.INSERT){try{Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);//通过反射为对象属性赋值setCreateTime.invoke(entity,now);setCreateUser.invoke(entity,currentid);setUpdateTime.invoke(entity,now);setUpdateUser.invoke(entity,currentid);} catch (Exception e) {e.printStackTrace();}}else if(operationType == OperationType.UPDATE) {//为2个公共字段赋值try {Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);//通过反射为对象属性赋值setUpdateTime.invoke(entity, now);setUpdateUser.invoke(entity, currentid);} catch (Exception e) {e.printStackTrace();}}}
}

在mapper上增加自定义注解autofill

com.sky.mapper.CategoryMapper

    @AutoFill(value = OperationType.INSERT)void insert(Category category);@AutoFill(value = OperationType.UPDATE)void update(Category category);

com.sky.mapper.EmployeeMapper

 	@AutoFill(value = OperationType.INSERT)void insert(Employee employee);@AutoFill(value = OperationType.UPDATE)void update(Employee employee);

删除/注释掉业务层原先为公共字段赋值的语句

com.sky.service.impl.CategoryServiceImpl

/*** 分类业务层*/
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {@Autowiredprivate CategoryMapper categoryMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 新增分类* @param categoryDTO*/public void save(CategoryDTO categoryDTO) {Category category = new Category();//属性拷贝BeanUtils.copyProperties(categoryDTO, category);//分类状态默认为禁用状态0category.setStatus(StatusConstant.DISABLE);//设置创建时间、修改时间、创建人、修改人
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());categoryMapper.insert(category);}/*** 分页查询* @param categoryPageQueryDTO* @return*/public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());//下一条sql进行分页,自动加入limit关键字分页Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);return new PageResult(page.getTotal(), page.getResult());}/*** 根据id删除分类* @param id*/public void deleteById(Long id) {//查询当前分类是否关联了菜品,如果关联了就抛出业务异常Integer count = dishMapper.countByCategoryId(id);if(count > 0){//当前分类下有菜品,不能删除throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);}//查询当前分类是否关联了套餐,如果关联了就抛出业务异常count = setmealMapper.countByCategoryId(id);if(count > 0){//当前分类下有菜品,不能删除throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);}//删除分类数据categoryMapper.deleteById(id);}/*** 修改分类* @param categoryDTO*/public void update(CategoryDTO categoryDTO) {Category category = new Category();BeanUtils.copyProperties(categoryDTO,category);//        //设置修改时间、修改人
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());categoryMapper.update(category);}/*** 启用、禁用分类* @param status* @param id*/public void startOrStop(Integer status, Long id) {Category category = Category.builder().id(id).status(status)
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId()).build();categoryMapper.update(category);}/*** 根据类型查询分类* @param type* @return*/public List<Category> list(Integer type) {return categoryMapper.list(type);}
}

com.sky.service.impl.EmployeeServiceImpl

@Service
public class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;/*** 员工登录** @param employeeLoginDTO* @return*/public Employee login(EmployeeLoginDTO employeeLoginDTO) {String username = employeeLoginDTO.getUsername();String password = employeeLoginDTO.getPassword();//1、根据用户名查询数据库中的数据Employee employee = employeeMapper.getByUsername(username);//2、处理各种异常情况(用户名不存在、密码不对、账号被锁定)if (employee == null) {//账号不存在throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);}//密码比对//后端md5加密password = DigestUtils.md5DigestAsHex(password.getBytes());if (!password.equals(employee.getPassword())) {//密码错误throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);}if (employee.getStatus() == StatusConstant.DISABLE) {//账号被锁定throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);}System.out.println("password"+password);System.out.println("username"+username);//3、返回实体对象return employee;}/*** 员工登录** @param employeeDTO* @return*/public void save(EmployeeDTO employeeDTO) {Employee employee = new Employee();BeanUtils.copyProperties(employeeDTO, employee);employee.setStatus(StatusConstant.ENABLE);employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        employee.setCreateUser(BaseContext.getCurrentId());
//        employee.setUpdateUser(BaseContext.getCurrentId());employeeMapper.insert(employee);}/*** 分页查询* @param employeePageQueryDTO* @return*/public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);long total = page.getTotal();List<Employee> records = page.getResult();return new PageResult(total, records);}/*** 启用禁用员工账号*/public void startOrStop(Integer status, Long id){Employee employee = Employee.builder().id(id).status(status).build();employeeMapper.update(employee);}/*** 根据id查员工* @param id* @return*/public Employee getById(Long id){Employee employee = employeeMapper.getById(id);employee.setPassword("****");return employee;}/*** 编辑员工* @param employeeDTO*/public void update(EmployeeDTO employeeDTO){Employee employee = new Employee();BeanUtils.copyProperties(employeeDTO, employee);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getCurrentId());employeeMapper.update(employee);}
}

新增菜品

接口设计

不懂为什么要设计成逻辑外键,而不是物理外键。

业务规则:

  • 菜品名称必须是唯一的
  • 菜品必须属于某个分类下,不能单独存在
  • 新增菜品时可以根据情况选择菜品的口味
  • 每个菜品必须对应一张图片

oss(两种方式)

看https://www.bilibili.com/video/BV1m84y1w7Tb的p147-p149就可以了。

在这里插入图片描述

在这里插入图片描述
bucket名称是唯一性的,所以很有可能出现“该名称已存在或已被其他占用”的情况,换个名字就好了。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

http://www.lryc.cn/news/482814.html

相关文章:

  • Go 语言中,golang结合 PostgreSQL 、MySQL驱动 开启数据库事务
  • Git核心概念
  • 网络安全技术在能源领域的应用
  • 这些场景不适合用Selenium自动化!看看你踩过哪些坑?
  • PHP反序列化靶场(php-SER-libs-main 第一部分)
  • 基于大数据爬虫+Python+SpringBoot+Hive的网络电视剧收视率分析与可视化平台系统(源码+论文+PPT+部署文档教程等)
  • DHCP与FTP
  • 云渲染与云电脑,应用场景与技术特点全对比
  • RockPI 4A单板Linux 4.4内核下的RK3399 GPIO功能解析
  • 【Vue】Vue3.0(二十三)Vue3.0中$attrs 的概念和使用场景
  • RHEL/CENTOS 7 ORACLE 19C-RAC安装(纯命令版)
  • CCSK:面试云计算岗的高频问题
  • C++ String(1)
  • ts 中 ReturnType 作用
  • Hadoop + Hive + Apache Ranger 源码编译记录
  • Java从入门到精通笔记篇(十二)
  • 入侵排查之Linux
  • 从0开始学习Linux——文件管理
  • 全面介绍软件安全测试分类,安全测试方法、安全防护技术、安全测试流程
  • Leidenアルゴリズムの詳細解説:Pythonによるネットワーク分割の実装
  • 安当ASP系统:适合中小企业的轻量级Radius认证服务器
  • Vue 组件间传值指南:Vue 组件通信的七种方法
  • 推荐一个超漂亮ui的网页应用设计
  • 有什么初学算法的书籍推荐?
  • 自动化工作流建设指南
  • [免费]SpringBoot+Vue3校园宿舍管理系统(优质版)【论文+源码+SQL脚本】
  • SNK施努卡 - 机器人测温取样系统
  • goframe开发一个企业网站 验证码17
  • 【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
  • MySQL电商多级分类表设计方案对比