您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍MybatisPlus如何实现插入或更新数据时自动填充更新数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency>
/**
* 基础Bean
*/
@Data
public class BaseEntity implements Serializable {
@TableField(value = "create_user", fill = FieldFill.INSERT) // 新增执行
private String createUser;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(value = "update_user", fill = FieldFill.INSERT_UPDATE) // 新增和更新执行
private String updateUser;
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
@TableField(value = "remark")
private String remark;
}@Data
@TableName("sys_dept")
public class SysDeptEntity extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 部门ID
**/
@TableId
private Long deptId;
/**
* 部门父节点ID
**/
private Long parentId;
/**
* 部门名称
**/
private String deptName;
/**
* 显示顺序
**/
private Integer orderNum;
/**
* 用户状态(0:正常 1:禁用)
**/
private Integer status;
@TableField(exist = false)
private List<SysDeptEntity> children;
}/**
* @author ShenTuZhiGang
* @version 1.0.0
* @date 2020-11-26 15:52
*/
@Slf4j
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {
@Autowired
private AuthenticationTrustResolver authenticationTrustResolver;
@Override
public void insertFill(MetaObject metaObject) {
log.info("come to insert fill .........");
this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(!authenticationTrustResolver.isAnonymous(authentication) && authentication!=null){
AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal();
this.setFieldValByName("createUser", user.getUsername(), metaObject);
this.setFieldValByName("updateUser", user.getUsername(), metaObject);
}else{
this.setFieldValByName("createUser", "unknown", metaObject);
this.setFieldValByName("updateUser", "unknown", metaObject);
}
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("come to update fill .........");
this.setFieldValByName("update_time", LocalDateTime.now(), metaObject);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(!authenticationTrustResolver.isAnonymous(authentication) && authentication!=null){
AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal();
this.setFieldValByName("updateUser", user.getUsername(), metaObject);
}else{
this.setFieldValByName("updateUser", "unknown", metaObject);
}
}
}不需要以下代码:
@Configuration
public class MyBatisPlusConfig {
/**
* 自动填充功能
* @return
*/
@Bean
public GlobalConfig globalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setMetaObjectHandler(new MetaHandler());
return globalConfig;
}
}@RequiresPermissions("sys:dept:add")
@PostMapping("/add")
@ResponseBody
public R add(@RequestBody SysDeptEntity deptEntity) {
logger.info("添加信息={}", deptEntity);
sysDeptService.save(deptEntity); // 不再需要设置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代码更优美
return R.ok();
}以上是“MybatisPlus如何实现插入或更新数据时自动填充更新数据”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。