MybatisPlus 插入或更新数据时自动填充更新数据解决方案
程序员文章站
2022-06-16 22:26:20
目录2、拦截器metaobjecthandlermaven org.springframework.boot
解决方案
参考文章
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自动添加数据库表中的创建时间、创建者、更新时间、更新者
到此这篇关于mybatisplus 插入或更新数据时自动填充更新数据解决方案的文章就介绍到这了,更多相关mybatisplus 插入或更新自动填充内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Python 循环函数详细介绍
下一篇: Python实现打乒乓小游戏