当前位置: 首页 > news >正文

spring boot加mybatis puls实现,在新增/修改时,对某些字段进行处理,使用的@TableField()或者AOP @Before

1.先说场景,在对mysql数据库表数据插入或者更新时都得记录时间和用户id

传统实现有点繁琐,这里还可以封装一下公共方法。

2.解决方法:

    2.1:使用aop切面编程(记录一下,有时间再攻克)。

        2.1.1:成功实现进行补充,感谢网友顶力相助(进步·于辰)

         2.1.2:在启动器上加   @EnableAspectJAutoProxy

         2.1.3:切入主要代码

@Aspect
@Component
@Slf4j
public class EntityAttrAspect {@Before("execution(* com.zuodou..*.save*(..))"+" ||execution(* com.zuodou..*.update*(..))")public void before(JoinPoint joinPoint) throws Exception {log.info("切入");String userId = BaseUtlis.getCurrentUser().getId();// 管理员idObject[] args = joinPoint.getArgs();for (Object arg : args) {if (arg != null) {Class<?> clazz = arg.getClass();// 获取实体所有属性,进而获取主键属性。一般情况下,主键属性是第一个Class<?> entityClass = clazz; // 使用clazz获取实体类的类型Field[] entityFieldArr = entityClass.getDeclaredFields();Field idField = entityFieldArr[0];// 主键属性idField.setAccessible(true);//设置为可以访问Object id = idField.get(arg); // 使用arg获取实体对象的主键值if (id == null) {// 无主键,插入if (StringUtils.isNotBlank(userId)){entityAttrIoc(arg, "createBy", userId);// 创建人}entityAttrIoc(arg, "createTime", new Date());// 创建时间} else {// 有主键,更新if (StringUtils.isNotBlank(userId)){entityAttrIoc(arg, "updateBy", userId);// 修改人}entityAttrIoc(arg, "updateTime", new Date());// 更新时间}}}}// entityAttrIoc方法的定义可以是类似于下面这样的形式:private void entityAttrIoc(Object entity, String attributeName, Object value) throws Exception {Field field = entity.getClass().getDeclaredField(attributeName);field.setAccessible(true);field.set(entity, value);}}

    注:

@Before("execution(* com.zuodou..*.save*(..))"+ " ||execution(* com.zuodou..*.update*(..))" )

切入点的表达式,不懂了可以先去了解表达式配置

我只需要判断新增还是修改,

Field idField = entityFieldArr[0];// 主键属性
idField.setAccessible(true);//设置为可以访问

我主键是由  file  和  static  修饰的  所以得加setAccessible

    2.2:使用@TableField()注解。

3.我使用的@TableField  注解实现,因为没有很多的业务处理,单纯的记录一下,以下是具体实现

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {// 在插入时自动填充 create_time 和 update_time 字段this.strictInsertFill(metaObject, "createTime", Date.class, new Date());this.strictInsertFill(metaObject, "createBy", String.class, BaseUtlis.getCurrentUser().getId());}@Overridepublic void updateFill(MetaObject metaObject) {// 在更新时自动填充 update_time 字段this.strictUpdateFill(metaObject, "updateTime",Date.class, new Date());this.strictInsertFill(metaObject, "updateBy", String.class, BaseUtlis.getCurrentUser().getId());}
}

注: 字段需要跟实体类对应起来,我项目架构是使用了驼峰命名使用下划线后字母大写。BaseUtlis.getCurrentUser().getId(),这个是我封装的一个获取当前登录用户的方法。

参数解析:

this.strictUpdateFill(metaObject, "updateTime",Date.class, new Date());

metaObject:对象

updateTime:实体类目标字段

Date.class:数据类型

new Date():具体值

4.yml配置

mybatis-plus:global-config:db-config:meta-object-handler: com.zuodou.mymeta.MyMetaObjectHandler

5.在对应实体类的字段上加

@TableField(fill = FieldFill.UPDATE)

或者

@TableField(fill = FieldFill.INSERT)

6.补充缺陷

直接在controllerdi调用根据id修改方法   或者 update 批量根据id修改, 会不触发自动填充

解决使用update,条件传入id或者在service调用,而updateBatchById就需要手动传了。

至于为什么在service调用updateById就能触发

在 MyBatis Plus 中,通常通过继承 ServiceImpl 类来实现 Service 层的操作。这个类提供了一些默认的 CRUD(增删改查)方法,并且默认情况下会使用 MyBatis Plus 的内置功能,比如自动填充。
当你在自己的 Service 类中继承了 ServiceImpl 并且指定了泛型类型,比如 YourService extends ServiceImpl&lt;YourMapper, YourEntity&gt;,这样就将 YourService 和 YourEntity 关联起来了。
MyBatis Plus 的 ServiceImpl 已经预先实现了一些常见的操作方法,其中包括了自动填充的支持。在这个类中,如果你调用了 updateById 方法,它内部会调用 MyBatis Plus 的自动填充逻辑,以便在更新数据时触发自动填充。
当然,前提是你需要做以下几件事情:

1.确保你的实体类中的字段有正确地标注了自动填充的注解,比如使用了 @TableField 注解并设置了相应的 fill 属性。
2.确保你已经正确配置了 MetaObjectHandler,并且这个配置被正确地注册到了 Spring 容器中。这样 MyBatis Plus 才能正确地使用自动填充功能。

因此,当你在自定义的 Service 类中继承了 ServiceImpl,就相当于在你的 Service 类中内置了 MyBatis Plus 提供的默认实现,包括自动填充的支持。这样,在调用 updateById 等方法时,会自动触发 MyBatis Plus 的自动填充逻辑。

(侵权联系删除)

http://www.lryc.cn/news/238774.html

相关文章:

  • 我的创作纪念日2048天
  • MatrixOne实战系列回顾 | 导入导出项目场景实践
  • Find My音箱|苹果Find My技术与音箱结合,智能防丢,全球定位
  • 51单片机应用
  • 系列三、ThreadLocal vs synchronized
  • 封装Redis工具类
  • 使用 millis() 函数作为延迟的替代方法(电位器控制延迟时间)
  • MySQL之BETWEEN AND包含范围查询总结
  • 4.3、Linux进程(2)
  • element-ui组件输入框之放大镜(搜索图标)
  • [oeasy]python001_先跑起来_python_三大系统选择_windows_mac_linux
  • 吴恩达《机器学习》9-4-9-6:实现注意:展开参数、梯度检验、随机初始化
  • 软信天成:如何利用大数据提高客户体验?
  • Vue 路由缓存 防止路由切换数据丢失 路由的生命周期
  • 基于ubuntu20.04安装ros系统搭配使用工业相机
  • 网络运维与网络安全 学习笔记2023.11.20
  • 银行业数据分析算法应用汇总
  • 搜索引擎trick:成为搜索高手的秘籍
  • 基于springboot实现冬奥会科普平台系统【项目源码+论文说明】计算机毕业设计
  • 用C++标准库生成制定范围内的整数随机数
  • 使用ExLlamaV2量化并运行EXL2模型
  • 【ARM AMBA AXI 入门 15 -- AXI-Lite 详细介绍】
  • 【开源】基于Vue.js的天然气工程业务管理系统的设计和实现
  • SpringBoot : ch04 整合数据源
  • Docker Swarm总结
  • 特殊token的特殊用途
  • 苹果Siri怎么打开?教你两招轻松唤醒!
  • 分类问题的评价指标
  • Hive 定义变量 变量赋值 引用变量
  • 51单片机LED灯渐明渐暗实验