[苍穹外卖]-03分类管理模块开发

效果预览

需求分析

查看产品原型: 根据原型分析业务规则

业务规则

  1. 分类名称必须是唯一的
  2. 分类按照类型可以分为菜品分类和套餐分类
  3. 新添加的分类状态默认为"禁用"

接口设计

该模块涉及6个接口: 新增分类/分类分页查询/根据id删除分类/修改分类/启用禁用分类/根据类型查询分类

数据表设计

category分类表设计

代码导入

导入代码: day02/资料/分类管理模块功能代码, 建议按照执行顺序从后往前导入代码文件, 减少代码报错

手动编译: 复制到工程的代码, 有时不会自动编译, 手动编译后再启动项目, 更加保险

联调测试

公共字段填充

问题分析

业务表中的公共字段, 需要我们手动维护, 造成业务中出现冗余的代码, 也不便于项目的维护

实现思路

  1. 自定义注解AutoFill, 用于标识需要进行公共字段填充的方法
  2. 自定义切面类AutoFillAspect, 统一拦截加入了 AutoFill注解的方法, 通过反射为公共字段赋值
  3. 在Mapper 的方法中按需加入 AutoFill注解
  4. 涉及到的技术点: 枚举/自定义注解/AOP/反射

代码开发

自定义注解: 定义AutoFill注解, 用于标识需要公共字段填充的方法

 * 数据库操作类型
 */
public enum OperationType {

    /**
     * 更新操作
     */
    UPDATE,

    /**
     * 插入操作
     */
    INSERT

}
@Target(ElementType.METHOD) //指定注解生效的类型
@Retention(RetentionPolicy.RUNTIME) //指定注解生效的时机
public @interface AutoFill {
    // 指定数据库操作类型: UPDATE INSERT
    OperationType value();
}

自定义切面类 AutoFillAspect, 统一拦截加入了AutoFill注解的方法, 通过反射为公共字段赋值

@Aspect //定义切面类
@Component //IOC控制反转
@Slf4j //记录日志
public class AutoFillAspect {
    /**
     * 切入点表达式
     * 拦截mapper包下的所有接口所有方法
     * 并且带有AutoFill注解的方法
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知,在通知中进行公共字段的赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(){
        log.info("开始进行公共字段自动填充...");
    }
}
package com.sky.constant;

/**
 * 公共字段自动填充相关常量
 */
public class AutoFillConstant {
    /**
     * 实体类中的方法名称
     */
    public static final String SET_CREATE_TIME = "setCreateTime";
    public static final String SET_UPDATE_TIME = "setUpdateTime";
    public static final String SET_CREATE_USER = "setCreateUser";
    public static final String SET_UPDATE_USER = "setUpdateUser";
}
/**
 * 自定义切面类, 实现公共字段自动填充的业务
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {
    /**
     * 定义切入点
     */
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {
    }

    /**
     * 定义前置通知, 在通知中进行公共字段的赋值
     */
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始进行公共字段自动赋值...");
        // 1.获取当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature(); // 方法签名对象
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class); // 获取方法上的注解对象
        OperationType operationType = autoFill.value(); // 获得数据库操作类型

        // 2.获取当前被拦截的方法的参数---实体对象
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        Object entity = args[0];

        // 3.准备赋值的数据
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        // 4.根据不同的操作类型, 为对应的属性通过反射来赋值
        if (operationType == OperationType.INSERT) {
            // 为4个公共字段赋值
            try {
                // 通过反射, 获取实体类中的set方法
                // getDeclaredMethod(方法名称, 方法参数类型);
                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);

                // 调用set方法为对象的属性赋值
                setCreateTime.invoke(entity, now);
                setCreateUser.invoke(entity, currentId);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } 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) {
                throw new RuntimeException(e);
            }
        }
    }
}

添加注解: 在Mapper的新增方和修改方法上加入AutoFill注解

/**
  * 插入员工数据
  *
  * @param employee
*/
@Insert("insert into employee (name,username,password,phone,sex,id_number,status,create_time,update_time,create_user,update_user)" +
        "values" +
        "(#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{status},#{createTime},#{updateTime},#{createUser},#{updateUser})")
@AutoFill(value = OperationType.INSERT)
void insert(Employee employee);
  1. 注解添加完成后可以把Service中的填充字段的操作删除

功能测试: 通过前后端联调查看新增和修改修改, 是否能够自动填充公共字段数据

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值