0
点赞
收藏
分享

微信扫一扫

如何使用MyBatis-Plus实现字段的自动填充?一文教会你

环境说明:Windows10 + Idea2021.3.2 + Jdk1.8 + SpringBoot 2.3.1.RELEASE

前言

在实际的开发过程中,我们经常需要在进行数据库操作时自动填充某些字段值,比如创建时间,更新时间等。手动填充虽然可行,但是容易出错,并且代码冗余,影响开发效率。MyBatis-Plus提供了字段自动填充的功能,可以在插入和更新操作时自动填充指定的字段值。

本篇文章将介绍如何使用MyBatis-Plus实现字段的自动填充。

摘要

本篇文章将会按照以下步骤来完成字段自动填充的功能:

  1. 引入MyBatis-Plus依赖
  2. 创建实体类并使用注解指定需要自动填充的字段
  3. 创建自定义的字段填充处理器
  4. 在MyBatis-Plus的配置类中配置字段填充处理器

正文

引入MyBatis-Plus依赖

首先,我们需要在maven中引入MyBatis-Plus的依赖:

<!-- mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.2</version>
</dependency>

创建实体类并使用注解指定需要自动填充的字段

接着,我们在实体类中定义需要自动填充的字段,并使用注解进行标记。这里我们以一个用户表为例,定义了创建时间和更新时间两个字段。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User {
private Long id;
private String name;

@TableField(fill = FieldFill.INSERT)
private Date createTime;

@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}

其中,注解@TableField的属性fill指定了填充类型,有以下几种常见的选择:

  1. FieldFill.INSERT:插入时填充字段
  2. FieldFill.UPDATE:更新时填充字段
  3. FieldFill.INSERT_UPDATE:插入和更新时均填充字段

创建自定义的字段填充处理器

接着,我们需要创建自定义的字段填充处理器,实现在插入和更新操作时自动填充指定的字段值。这里我们以创建时间和更新时间为例,创建CustomFieldFillHandler类并继承MetaObjectHandler类。

@Component
public class CustomFieldFillHandler extends MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName(createTime, new Date(), metaObject);
this.setFieldValByName(updateTime, new Date(), metaObject);
}

@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName(updateTime, new Date(), metaObject);
}
}

在这里,我们重写了MetaObjectHandler的insertFill和updateFill方法,并在其中指定了需要填充的字段名称和值。

在MyBatis-Plus的配置类中配置字段填充处理器

最后,我们需要在MyBatis-Plus的配置类中配置字段填充处理器,使其生效。首先,我们创建MyBatisPlusConfig类,并在其中使用@Bean注解定义CustomFieldFillHandler的Bean实例。

@Configuration
public class MyBatisPlusConfig {
@Bean
public CustomFieldFillHandler customFieldFillHandler() {
return new CustomFieldFillHandler();
}
}

接着,我们在配置类中使用注解进行配置:

@Configuration
@MapperScan(com.example.demo.mapper)
@ServletComponentScan
public class MyBatisPlusConfig {

@Bean
public CustomFieldFillHandler customFieldFillHandler() {
return new CustomFieldFillHandler();
}

/**
* 配置MyBatis-Plus的全局设置
*/

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
// 字段填充插件
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor());
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
// 自定义插件
interceptor.addInnerInterceptor(new CustomInterceptor());
return interceptor;
}
}

在这里,我们使用MybatisPlusInterceptor类来进行插件的配置,并使用addInnerInterceptor方法添加了OptimisticLockerInnerInterceptor(乐观锁插件)、TenantLineInnerInterceptor(多租户插件)、BlockAttackInnerInterceptor(防SQL注入插件)等插件。最重要的是我们添加了CustomInterceptor(自定义插件),在其中指定了CustomFieldFillHandler的Bean实例。

至此,我们完成了MyBatis-Plus字段自动填充的功能实现。

测试用例

为了验证字段自动填充的功能是否正常工作,我们可以新建一个UserController,编写以下代码:

@RestController
@RequestMapping(/user)
public class UserController {
@Autowired
private UserService userService;

@PostMapping(/add)
public String add(@RequestBody User user) {
userService.save(user);
return add success;
}

@GetMapping(/get/{id})
public User get(@PathVariable Long id) {
return userService.getById(id);
}

@GetMapping(/list)
public List<User> list() {
return userService.list();
}
}

运行程序,使用Postman测试添加用户的接口,可以看到在插入操作时,create_time和update_time字段的值已经自动填充了:

==>  Preparing: INSERT INTO user ( id, create_by, create_time, update_by, name, update_time ) VALUES ( ?, ?, ?, ?, ?, ? ) 
==> Parameters: ad7a97a8ca1533423d5d06f653c43e79(String), xxx(String), 2022-03-29 00:23:09.355(Timestamp), xxx(String), 我是bug菌(String), 2022-03-29 00:23:09.355(Timestamp)

修改数据测试截图如下:

在这里插入图片描述

可以看到create_time及update_by自动被自动填写了。

小结

本篇文章介绍了如何使用MyBatis-Plus实现字段自动填充的功能,主要分为以下几个步骤:

  1. 引入MyBatis-Plus依赖
  2. 创建实体类并使用注解指定需要自动填充的字段
  3. 创建自定义的字段填充处理器
  4. 在MyBatis-Plus的配置类中配置字段填充处理器

通过以上步骤,我们可以方便地实现数据库操作时的字段自动填充。

附录源码

  如上涉及所有源码均已上传同步在「GitHub」,提供给同学们一对一参考学习,辅助你更迅速的掌握。

总结

本文主要介绍了如何使用MyBatis-Plus实现字段自动填充功能。在实际的开发过程中,我们经常需要在进行数据库操作时自动填充某些字段值,比如创建时间,更新时间等。手动填充虽然可行,但容易出错,并且代码冗余,影响开发效率。使用MyBatis-Plus提供的字段自动填充功能,可以在插入和更新操作时自动填充指定的字段值,减少手动填充的工作量,提高开发效率。

具体实现步骤如下:

  1. 引入MyBatis-Plus依赖;

  2. 创建实体类并使用注解指定需要自动填充的字段;

  3. 创建自定义的字段填充处理器,实现在插入和更新操作时自动填充指定的字段值;

  4. 在MyBatis-Plus的配置类中配置字段填充处理器,使其生效。

通过以上步骤,我们即可实现数据库操作时的字段自动填充。在实际开发中,我们可以根据具体需求,指定需要自动填充的字段类型和填充方式,从而提高开发效率和代码质量。

举报

相关推荐

0 条评论