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>
解决方案
1、 实体类
/** * 基础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; }
2、拦截器MetaObjectHandler
/** * @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; } }
3、测试
@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(); }
参考文章
使用MyBatis Plus自动添加数据库表中的创建时间、创建者、更新时间、更新者
使用mybatis plus插入或更新数据时mysql表的添加时间和更新时间字段没有更新(mybatis plus坑之一)
到此这篇关于MybatisPlus 插入或更新数据时自动填充更新数据解决方案的文章就介绍到这了,更多相关MybatisPlus 插入或更新自动填充内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot自动配置@EnableAutoConfiguration过程示例
这篇文章主要为大家介绍了SpringBoot自动配置@EnableAutoConfiguration的过程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-10-10@FeignClient的使用和Spring Boot的版本适配方式
这篇文章主要介绍了@FeignClient的使用和Spring Boot的版本适配方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03kafka-console-consumer.sh使用2次grep管道无法提取消息的解决
这篇文章主要介绍了kafka-console-consumer.sh使用2次grep管道无法提取消息的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-03-03解决SSLContext.getInstance()中参数设置TLS版本无效的问题
这篇文章主要介绍了解决SSLContext.getInstance()中参数设置TLS版本无效的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-01-01SpringBoot @RestControllerAdvice注解对返回值统一封装的处理方法
这篇文章主要介绍了SpringBoot @RestControllerAdvice注解对返回值统一封装,使用@RestControllerAdvice对响应进行增强,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-09-09如何解决Field name doesn‘t have a defau
这篇文章主要介绍了如何解决Field name doesn‘t have a default value报错问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-02-02
最新评论