当前位置: 首页> 房产> 政策 > 开发公司给物业公司的通知函_广州疫情防控_百度推广方式_如何在网上做销售推广

开发公司给物业公司的通知函_广州疫情防控_百度推广方式_如何在网上做销售推广

时间:2025/7/13 8:59:47来源:https://blog.csdn.net/weixin_52165318/article/details/145556578 浏览次数:1次
开发公司给物业公司的通知函_广州疫情防控_百度推广方式_如何在网上做销售推广

1、公共字段自动填充

需求分析:业务表的create_time、create_user、update_time、update_user为公共字段,因此我们可以自定义切面类,使用注解注入的方法,通过反射为公共字段赋值。

步骤:
1、自定义注解AutoFill,用于标识需要进行公共字段自动填充的方法;
2、自定义切面类AutoFillAspect,统一拦截加入了AutoFill注解的方法,通过反射为公共字段赋值;
3、在Mapper方法上加入Autofill注解。
技术点:
枚举、注解、AOP、反射

1、自定义注解@Autofill

在这里插入图片描述

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {// 数据库操作类型//定义注解的value属性OperationType value();
}

2、定义切面

(1)定义切入点

    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")public void autoFillPointCut() {}

(2)定义通知(增强)

    @Before("autoFillPointCut()")public void autoFill(JoinPoint joinPoint) {log.info("开始进行公共字段自动填充...");//joinPoint:execution(void com.sky.mapper.EmployeeMapper.update(Employee))//log.info("joinPoint: {}", joinPoint);//获取到当前被拦截方法上的数据库操作类型MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();//methodSignatue:void com.sky.mapper.EmployeeMapper.update(Employee)//log.info("methodSignature: {}", methodSignature);AutoFill autoFill = methodSignature.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();//根据不同操作类型,为对应的属性通过反射赋值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) {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();}}}

(3)在对应方法前添加注释

    @AutoFill(value = OperationType.INSERT)@Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +" VALUES" +" (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")void insert(Category category);
关键字:开发公司给物业公司的通知函_广州疫情防控_百度推广方式_如何在网上做销售推广

版权声明:

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

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

责任编辑: