SpringBoot项目中公共字段填充的实现
更新时间:2023年10月08日 16:10:32 作者:终有救赎
本文主要介绍了SpringBoot项目中公共字段填充的实现,利用SpringBoot的Aop思想和自定义注解和反射机制的方法来实现,具有一定的参考价值,感兴趣的可以了解一下
思路:
利用的是SpringBoot的Aop思想和自定义注解和反射机制的方法来实现
项目中我涉及公共字段的有createTime、updateTime、createUser、updateUser
步骤:
1. 自定义注解AutoFill,用于标识需要进行公共字段自动填充的方法
/**
* 数据库操作类型 使用的是枚举方法
*/
public enum OperationType {
/**
* 更新操作
*/
UPDATE,
/**
* 插入操作
*/
INSERT
}/**
* 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AutoFill {
//数据库操作类型:UPDATE INSERT
OperationType value();
}2. 自定义切面类AutoFillAspect,统一拦截加入了AutoFill注解的方法,通过反射为公共字段赋值
/**
* 自定义切面,实现公共字段字段填充处理逻辑
*/
@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("开始进行公共字段的填充...");
//获取到当前被拦截方法上的数据库操作类型
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
OperationType type = autoFill.value();
//获取到当前被拦截方法的参数--实体对象
Object[] args = joinPoint.getArgs();
if(args == null || args.length!=0){
return;
}
Object enity = args[0];
//准备赋值的数据
Long id = BaseContext.getCurrentId();
LocalDateTime localDateTime = LocalDateTime.now();
//根据当前不同的操作类型,为对应额属性通过反射来赋值
if(type == OperationType.INSERT){
//为四个公共字段赋值
try {
Method setCreateTime = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
Method setCreateUser = enity.getClass().getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
setCreateTime.invoke(enity,localDateTime);
setUpdateTime.invoke(enity,localDateTime);
setCreateUser.invoke(enity,id);
setUpdateUser.invoke(enity,id);
} catch (Exception e) {
e.printStackTrace();
}
}else if(type == OperationType.UPDATE){
//为两个公共字段赋值
try {
Method setUpdateTime = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class);
Method setUpdateUser = enity.getClass().getMethod(AutoFillConstant.SET_UPDATE_USER,Long.class);
setUpdateTime.invoke(enity,localDateTime);
setUpdateUser.invoke(enity,id);
} catch (Exception e) {
e.printStackTrace();
}
}
}3. 在Mapper的需要自动填充公共字段的方法上加入AutoFill注解
@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});")
@AutoFill(value = OperationType.INSERT)
void save(Category category);
@Delete("delete from category where id = #{id}")
void deleteById(Long id);
@AutoFill(value = OperationType.UPDATE)
void update(Category category);到此这篇关于SpringBoot项目中公共字段填充的实现的文章就介绍到这了,更多相关SpringBoot公共字段填充内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Bean生命周期之Bean元信息的配置与解析阶段详解
这篇文章主要为大家详细介绍了Spring Bean生命周期之Bean元信息的配置与解析阶段,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助2022-03-03
JAVA面试题 从源码角度分析StringBuffer和StringBuilder的区别
这篇文章主要介绍了JAVA面试题 从源码角度分析StringBuffer和StringBuilder的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面我们来一起学习下吧2019-07-07
SpringBoot整合RedisTemplate实现缓存信息监控的步骤
这篇文章主要介绍了SpringBoot整合RedisTemplate实现缓存信息监控,一步一步的实现 Springboot 整合 Redis 来存储数据,读取数据,需要的朋友可以参考下2022-01-01
SpringCloud-Gateway转发WebSocket失败问题及解决
这篇文章主要介绍了SpringCloud-Gateway转发WebSocket失败问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-09-09


最新评论