5. 属性自动填充

《阿里巴巴Java开发手册》,在第 5 章 MySQL 数据库可以看到这样一条规范:
在这里插入图片描述

对于一张数据表,它必须具备三个字段:

  • id : 唯一ID
  • gmt_create : 保存的是当前数据创建的时间
  • gmt_modified : 保存的是更新时间

改造一下数据表:

alter table tbl_employee add column gmt_create datetime not null;
alter table tbl_employee add column gmt_modified datetime not null;

然后改造一下实体类:

@Data
@TableName("tbl_employee")
public class Employee {

    @TableId(type = IdType.AUTO) // 设置主键策略
    private Long id;
    private String lastName;
    private String email;
    private Integer age;
    private LocalDateTime gmtCreate;
    private LocalDateTime gmtModified;
}

此时在插入数据和更新数据的时候就需要手动去维护这两个属性:

@Test
void contextLoads() {
    Employee employee = new Employee();
    employee.setLastName("lisa");
    employee.setEmail("lisa@qq.com");
    employee.setAge(20);
    // 设置创建时间
    employee.setGmtCreate(LocalDateTime.now());
    employee.setGmtModified(LocalDateTime.now());
    employeeService.save(employee);
}

@Test
void contextLoads() {
    Employee employee = new Employee();
    employee.setId(1385934720849584130L);
    employee.setAge(50);
    // 设置创建时间
    employee.setGmtModified(LocalDateTime.now());
    employeeService.updateById(employee);
}

每次都需要维护这两个属性未免过于麻烦,好在 MyBatisPlus 提供了字段自动填充功能来帮助我们进行管理,需要使用到的是 @TableField 注解:

@Data
@TableName("tbl_employee")
public class Employee {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String lastName;
    private String email;
    private Integer age;
    @TableField(fill = FieldFill.INSERT) // 插入的时候自动填充
    private LocalDateTime gmtCreate;
    @TableField(fill = FieldFill.INSERT_UPDATE) // 插入和更新的时候自动填充
    private LocalDateTime gmtModified;
}

然后编写一个类实现 MetaObjectHandler 接口:

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 实现插入时的自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insert开始属性填充");
        this.strictInsertFill(metaObject,"gmtCreate", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject,"gmtModified", LocalDateTime.class,LocalDateTime.now());
    }

    /**
     * 实现更新时的自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("update开始属性填充");
        this.strictInsertFill(metaObject,"gmtModified", LocalDateTime.class,LocalDateTime.now());
    }
}

该接口中有两个未实现的方法,分别为插入和更新时的填充方法,在方法中调用 strictInsertFill() 方法 即可实现属性的填充,它需要四个参数:

  • metaObject:元对象,就是方法的入参
  • fieldName:为哪个属性进行自动填充
  • fieldType:属性的类型
  • fieldVal:需要填充的属性值

此时在插入和更新数据之前,这两个方法会先被执行,以实现属性的自动填充,通过日志我们可以进行验证:

@Test
void hh(){
   Employee employee = new Employee();
   employee.setLastName("lisa");
   employee.setEmail("lisa@qq.com");
   employee.setAge(23);
   employeeService.updateById(employee);
}

在这里插入图片描述

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我超爱写bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值