一、介绍
MetaObjectHandler是MybatisPlus提供的一个扩展接口,使用这个接口可以在插入和更新时为一些自段设置默认值(类似自动填充策略)。文章来源地址https://www.toymoban.com/news/detail-700128.html
二、应用
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
this.strictInsertFill(metaObject, "status", Integer.class, 0);
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
LocalDateTime current = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : LocalDateTime.now();
baseEntity.setCreateTime(current);
baseEntity.setUpdateTime(current);
String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : getLoginUsername();
String nickname = StringUtils.isNotBlank(baseEntity.getCreateName())
? baseEntity.getCreateName() : getLoginNickName();
// 当前已登录 且 创建人为空 则填充
baseEntity.setCreateBy(username);
baseEntity.setCreateName(nickname);
// 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username);
baseEntity.setUpdateName(nickname);
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
@Override
public void updateFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
LocalDateTime current = LocalDateTime.now();
// 更新时间填充(不管为不为空)
baseEntity.setUpdateTime(current);
String username = getLoginUsername();
String nickname = getLoginNickName();
// 当前已登录 更新人填充(不管为不为空)
if (StringUtils.isNotBlank(username)) {
baseEntity.setUpdateBy(username);
}
if(StringUtils.isNotEmpty(nickname)) {
baseEntity.setUpdateName(nickname);
}
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
三、实体类加@TableField(fill = FieldFill.INSERT_UPDATE)
@Data
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 搜索值
*/
@JsonIgnore
@TableField(exist = false)
private String searchValue;
/**
* 创建者
*/
@TableField(fill = FieldFill.INSERT)
private String createBy;
/**
* 创建昵称
*/
@TableField(fill = FieldFill.INSERT)
private String createName;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新者
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 更新者昵称
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateName;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
/**
* 请求参数
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@TableField(exist = false)
private Map<String, Object> params = new HashMap<>();
}
四、config配置
@Bean
public MetaObjectHandler metaObjectHandler() {
return new CreateAndUpdateMetaObjectHandler();
}
文章来源:https://www.toymoban.com/news/detail-700128.html
到了这里,关于MybatisPlus自动填充-MetaObjectHandler接口的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!