概要
在新增员工或者新增菜品分类时需要设置创建时间、创建人、修改时间、修改人等字段,在编辑员工或者编辑菜品分类时需要设置修改时间、修改人等字段。这些字段属于公共字段,也就是也就是在我们的系统中很多表中都会有这些字段,如下:
序号 | 字段名 | 含义 | 数据类型 |
---|---|---|---|
1 | create_time | 创建时间 | datetime |
2 | create_user | 创建人id | bigint |
3 | update_time | 修改时间 | datetime |
4 | update_user | 修改人id | bigint |
而针对于这些字段,我们的赋值方式为:
1). 在新增数据时, 将createtime、updatetime 设置为当前时间, createuser、updateuser设置为当前登录用户id。
2). 在更新数据时, 将updatetime 设置为当前时间, updateuser设置为当前登录用户id。
我们使用aop切面编程,实现功能增强,来完成公共字段自动填充功能。
技术细节
在实现公共字段自动填充,也就是在插入或者更新的时候为指定字段赋予指定的值,使用它的好处就是可以统一对这些字段进行处理,避免了重复代码。在上述的问题分析中,我们提到有四个公共字段,需要在新增/更新中进行赋值操作, 具体情况如下:
序号 | 字段名 | 含义 | 数据类型 | 操作类型 |
---|---|---|---|---|
1 | create_time | 创建时间 | datetime | insert |
2 | create_user | 创建人id | bigint | insert |
3 | update_time | 修改时间 | datetime | insert、update |
4 | update_user | 修改人id | bigint | insert、update |
实现步骤:
1). 自定义注解 autofill,用于标识需要进行公共字段自动填充的方法
2). 自定义切面类 autofillaspect,统一拦截加入了 autofill 注解的方法,通过反射为公共字段赋值
3). 在 mapper 的方法上加入 autofill 注解
若要实现上述步骤,需掌握以下知识
技术点:枚举、注解、aop、反射
- 枚举类:
package com.sky.enumeration; /** * 数据库操作类型 */ public enum operationtype { /** * 更新操作 */ update, /** * 插入操作 */ insert }
- 自定义注解:
package com.sky.annotation; import com.sky.enumeration.operationtype; import java.lang.annotation.elementtype; import java.lang.annotation.retention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; /** * 自定义注解,用于标识某个方法需要进行功能字段自动填充处理 */ @target(elementtype.method) @retention(retentionpolicy.runtime) public @interface autofill { //数据库操作类型:update insert operationtype value(); }
- 自定义切面类:
- 公共字段填充,我们选择在连接点(方法)执行前就进行,所以用@before
- 将连接点上的@autofill注解拦截下
- 利用注解的.value()获取到注解中的内容(数据库操作类型)
- 获取到该连接点的参数
- 准备好填充的内容
- 利用反射根据数据库操作类型进行填充
package com.sky.aspect; import com.sky.annotation.autofill; import com.sky.constant.autofillconstant; import com.sky.context.basecontext; import com.sky.enumeration.operationtype; import lombok.extern.slf4j.slf4j; import org.aspectj.lang.joinpoint; import org.aspectj.lang.signature; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; import org.aspectj.lang.annotation.pointcut; import org.aspectj.lang.reflect.methodsignature; import org.springframework.stereotype.component; import java.lang.annotation.annotation; import java.lang.reflect.method; import java.time.localdatetime; @aspect @slf4j @component 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("进行公共字段赋值"); //获取当前被拦截方法上的注解 autofill autofill = ((methodsignature) joinpoint.getsignature()).getmethod().getannotation(autofill.class); //methodsignature signature = (methodsignature) joinpoint.getsignature();//方法签名对象 //autofill autofill = signature.getmethod().getannotation(autofill.class); //获取数据库操作类型(insert,update) operationtype operationtype = autofill.value(); //获取当前被拦截方法上的参数(实体对象) object[] args = joinpoint.getargs(); if (args == null || args.length == 0) { return; } object entity = args[0]; //准备要填充的内容 localdatetime now = localdatetime.now(); long id = 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, id); setupdatetime.invoke(entity, now); setupdateuser.invoke(entity, id); } 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, id); } catch (exception e) { e.printstacktrace(); } } } }
@autofill(value = operationtype.insert) void insert(dish dish);
效果展示
到此这篇关于使用springaop实现公共字段填充功能的文章就介绍到这了,更多相关springaop公共字段填充内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论