MybatisPlus如何实现插入或更新数据时自动填充更新数据
发表于:2024-10-19 作者:千家信息网编辑
千家信息网最后更新 2024年10月19日,这篇文章主要介绍MybatisPlus如何实现插入或更新数据时自动填充更新数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Maven org.springframewo
千家信息网最后更新 2024年10月19日MybatisPlus如何实现插入或更新数据时自动填充更新数据解决方案
这篇文章主要介绍MybatisPlus如何实现插入或更新数据时自动填充更新数据,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Maven
org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE com.baomidou mybatis-plus-boot-starter 3.1.0
解决方案
1、 实体类
/** * 基础Bean */@Datapublic 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 Listchildren; }
2、拦截器MetaObjectHandler
/** * @author ShenTuZhiGang * @version 1.0.0 * @date 2020-11-26 15:52 */@Slf4j@Componentpublic 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); } }}
不需要以下代码:
@Configurationpublic class MyBatisPlusConfig { /** * 自动填充功能 * @return */ @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new MetaHandler()); return globalConfig; } }
3、测试
@RequiresPermissions("sys:dept:add")@PostMapping("/add")@ResponseBodypublic R add(@RequestBody SysDeptEntity deptEntity) { logger.info("添加信息={}", deptEntity); sysDeptService.save(deptEntity); // 不再需要设置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代码更优美 return R.ok();}
以上是"MybatisPlus如何实现插入或更新数据时自动填充更新数据"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
更新
数据
部门
代码
内容
篇文章
优美
价值
信息
兴趣
功能
名称
基础
实体
小伙
小伙伴
方案
更多
状态
用户
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
大庆冠通网络技术开发公司
小程序商城数据库用哪一个比较好
归档数据库如何设计
在工行总行做软件开发体验
三大数据库检索工具的区别
深圳鸿湾软件开发有限公司
服务器维护步骤
公司电脑向日葵连不上服务器
网络安全秘钥在哪里找
网络安全应急人员证书
数据库四大功能安全性完整性
知识产权数据库
绿色软件开发技术基础
固件软件开发
党委 服务器安全自查
软件开发费和软件许可
网络安全法违规
软件开发需求个人工作总结
计算机网络技术专业历史
win7 建服务器
数据库的表导出后有何不同
濠江网络安全与融媒体中心
成都生于有网络技术有限公司
中国网络安全产业网
app打车软件开发费用
软件开发顾问团队
2018年网络技术术语
苹果手机更改密码无法连接服务器
无锡智企网络技术有限公司
软件开发过程的三个阶段是