找回密码
 立即注册
查看: 8|回复: 0

springboot3公共字段自动填充,创建时间、创建人、修改时间、修改人等

[复制链接]

106

主题

5

精华

110

金币

技术维护QQ:515138

积分
242
发表于 前天 15:34 | 显示全部楼层 |阅读模式
springboot3公共字段自动填充,创建时间、创建人、修改时间、修改人等
序号
字段名
含义
数据类型

1create_time创建时间datetime
2create_user创建人idbigint
3update_time修改时间datetime
4update_user修改人idbigint1,新增自定义注解
  1. package com.zidiu.anno;
  2. import com.zidiu.enumeration.OperationType;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Retention;
  5. import java.lang.annotation.RetentionPolicy;
  6. import java.lang.annotation.Target;
  7. /**
  8. * 自定义注解,用于标识某个方法需要进行功能字段自动填充处理
  9. */
  10. @Target(ElementType.METHOD)
  11. @Retention(RetentionPolicy.RUNTIME)
  12. public @interface AutoFill {
  13.     //数据库操作类型:UPDATE INSERT
  14.     OperationType value();
  15. }
复制代码
2,自定义切面,实现公共字段自动填充处理逻辑
  1. package com.zidiu.aspect;
  2. import com.zidiu.anno.AutoFill;
  3. import com.zidiu.constant.AutoFillConstant;
  4. import com.zidiu.context.BaseContext;
  5. import com.zidiu.enumeration.OperationType;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.aspectj.lang.JoinPoint;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Before;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.springframework.stereotype.Component;
  13. import java.lang.reflect.Method;
  14. import java.time.LocalDateTime;
  15. /**
  16. * 自定义切面,实现公共字段自动填充处理逻辑
  17. */
  18. @Aspect
  19. @Component
  20. @Slf4j
  21. public class AutoFillAspect {
  22.     /**
  23.      * 切入点
  24.      */
  25.     @Pointcut("execution(* com.zidiu.mapper.*.*(..)) && @annotation(com.zidiu.anno.AutoFill)")
  26.     public void autoFillPointCut(){}
  27.     /**
  28.      * 前置通知,在通知中进行公共字段的赋值
  29.      */
  30.     @Before("autoFillPointCut()")
  31.     public void autoFill(JoinPoint joinPoint){
  32.         log.info("开始进行公共字段自动填充...");
  33.         //获取到当前被拦截的方法上的数据库操作类型
  34.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
  35.         AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);//获得方法上的注解对象
  36.         OperationType operationType = autoFill.value();//获得数据库操作类型
  37.         //获取到当前被拦截的方法的参数--实体对象,实体放第一个位置
  38.         Object[] args = joinPoint.getArgs();
  39.         if(args == null || args.length == 0){
  40.             return;
  41.         }
  42.         Object entity = args[0];
  43.         //准备赋值的数据
  44.         LocalDateTime now = LocalDateTime.now();
  45.         Long currentId = BaseContext.getCurrentId();
  46.         //根据当前不同的操作类型,为对应的属性通过反射来赋值
  47.         if(operationType == OperationType.INSERT){
  48.             //为4个公共字段赋值
  49.             try {
  50.                 Method setCreateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
  51.                 Method setCreateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
  52.                 Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
  53.                 Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
  54.                 //通过反射为对象属性赋值
  55.                 setCreateTime.invoke(entity,now);
  56.                 setCreateUser.invoke(entity,currentId);
  57.                 setUpdateTime.invoke(entity,now);
  58.                 setUpdateUser.invoke(entity,currentId);
  59.             } catch (Exception e) {
  60.                 e.printStackTrace();
  61.             }
  62.         }else if(operationType == OperationType.UPDATE){
  63.             //为2个公共字段赋值
  64.             try {
  65.                 Method setUpdateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
  66.                 Method setUpdateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
  67.                 //通过反射为对象属性赋值
  68.                 setUpdateTime.invoke(entity,now);
  69.                 setUpdateUser.invoke(entity,currentId);
  70.             } catch (Exception e) {
  71.                 e.printStackTrace();
  72.             }
  73.         }
  74.     }
  75. }
复制代码
3,枚举
  1. package com.zidiu.enumeration;
  2. /**
  3. * 数据库操作类型
  4. */
  5. public enum OperationType {
  6.     /**
  7.      * 更新操作
  8.      */
  9.     UPDATE,
  10.     /**
  11.      * 插入操作
  12.      */
  13.     INSERT
  14. }
复制代码
M层使用:
  1. package com.zidiu.mapper;
  2. import com.github.pagehelper.Page;
  3. import com.zidiu.anno.AutoFill;
  4. import com.zidiu.dto.CategoryPageQueryDTO;
  5. import com.zidiu.entity.Category;
  6. import com.zidiu.enumeration.OperationType;
  7. import org.apache.ibatis.annotations.Delete;
  8. import org.apache.ibatis.annotations.Insert;
  9. import org.apache.ibatis.annotations.Mapper;
  10. import java.util.List;
  11. @Mapper
  12. public interface CategoryMapper {
  13.     /**
  14.      * 新增分类
  15.      * @param category
  16.      */
  17.     @AutoFill(value = OperationType.INSERT)
  18.     @Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
  19.             " VALUES" +
  20.             " (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
  21.     void insert(Category category);
  22.     /**
  23.      * 分类分页查询
  24.      * @param categoryPageQueryDTO
  25.      * @return
  26.      */
  27.     Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
  28.     /**
  29.      * 根据id删除分类
  30.      * @param id
  31.      */
  32.     @Delete("delete from category where id = #{id}")
  33.     void deleteById(Long id);
  34.     /**
  35.      * 修改分类
  36.      * @param category
  37.      */
  38.     @AutoFill(value = OperationType.UPDATE)
  39.     void update(Category category);
  40.     /**
  41.      * 查询分类
  42.      * @param type
  43.      * @return
  44.      */
  45.     List<Category> list(Integer type);
  46. }
复制代码
jinhei-waimai.zip (433.47 KB, 下载次数: 0, 售价: 50 金币)


SpringBoot3+SSM的企业级Java项目实战之外卖小程序



上一篇:springboot3操作时间显示问题2025-12-18T14:35:36
下一篇:SpringBoot3之Redis缓存数据库redis操作和使用指南
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2025-12-21 16:47

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表